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]))

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?
source
share