I find that it is difficult to make the text aligned.
table=\'\'\'乘客姓名,性别,出生日期
HuangTianhui,男,1948/05/28
姜翠云,女,1952/03/27
李红晶,女,1994/12/09
LuiChing,女,1969/08
You could take into account the number of fullwidth vs halfwidth characters in the string:
#!/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]))
Output:
Not beautiful, but here's one way. (encode/decode 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: