How to control the output format when moving Chinese characters and ascii?

I find it difficult to align the text.

table='''乘客姓名,性别,出生日期
HuangTianhui,男,1948/05/28
姜翠云,女,1952/03/27
李红晶,女,1994/12/09
LuiChing,女,1969/08/02
宋飞飞,男,1982/03/01
唐旭东,男,1983/08/03
YangJiabao,女,1988/08/25
买买提江·阿布拉,男,1979/07/10
安文兰,女,1949/10/20
胡偲婠(婴儿),女,2011/02/25
(有待确定姓名),男,1985/07/20
'''
data=[ [cell  for cell in row.split(",") ] for row in table.split("\n")  if row]
len0=max([ len(x[0])   for x in data])
len1=max([ len(x[1])   for x in data])
len2=max([ len(x[2])   for x in data])

for cell in data:
    print("_"*((len0+len1+len2)*2+4) )
    print("|%24s|%4s|%20s|" % (cell[0],cell[1],cell[2]))

enter image description here

Not every line (12 + 2 + 10) * 2 + 4 character widths, the same format control statement, but different output, how can I fix it so that each line has the same output format, the same width?

+4
source share
2 answers

Not beautiful, but here is one way. (encoding / decoding with bytes.rjust)

table='''乘客姓名,性别,出生日期
HuangTianhui,男,1948/05/28
姜翠云,女,1952/03/27
李红晶,女,1994/12/09
LuiChing,女,1969/08/02
宋飞飞,男,1982/03/01
唐旭东,男,1983/08/03
YangJiabao,女,1988/08/25
提江·阿布拉,男,1979/07/10
安文兰,女,1949/10/20
胡偲婠(婴儿),女,2011/02/25
(有待确定姓名),男,1985/07/20
'''
table = table.encode('gb18030')
data = [[cell  for cell in row.split(b",")]
        for row in table.split(b"\n")  if row]
len0 = max([len(x[0]) for x in data])
len1 = max([len(x[1]) for x in data])
len2 = max([len(x[2]) for x in data])

for cell in data:
    print("_" * (len0+len1+len2+4))
    line = (
        b"|" + cell[0].rjust(len0) +
        b"|" + cell[1].rjust(len1) +
        b"|" + cell[2].rjust(len2) + b"|"
    )
    print(line.decode('gb18030'))

output:

enter image description here

+1
source

You can take into account the number of characters fullwidth vs halfwidth per line:

#!/usr/bin/python3
# coding=utf8

import unicodedata

def wide_chars(s):
    return sum(unicodedata.east_asian_width(x)=='W' for x in s)
def width(s):
    return len(s) + wide_chars(s)

table='''乘客姓名,性别,出生日期
HuangTianhui,男,1948/05/28
姜翠云,女,1952/03/27
李红晶,女,1994/12/09
LuiChing,女,1969/08/02
宋飞飞,男,1982/03/01
唐旭东,男,1983/08/03
YangJiabao,女,1988/08/25
买买提江·阿布拉,男,1979/07/10
安文兰,女,1949/10/20
胡偲婠(婴儿),女,2011/02/25
(有待确定姓名),男,1985/07/20
'''
data=[ [cell  for cell in row.split(",") ] for row in table.split("\n")  if row]
len0=max([ width(x[0])   for x in data])
len1=max([ width(x[1])   for x in data])
len2=max([ width(x[2])   for x in data])

for cell in data:
    print("_"*((len0+len1+len2)*2+4) )
    print("|%*s|%*s|%*s|" % (24-wide_chars(cell[0]), cell[0],
                               4-wide_chars(cell[1]),cell[1],
                               20-wide_chars(cell[2]), cell[2]))

Conclusion:

enter image description here

+5
source

Source: https://habr.com/ru/post/1538249/


All Articles