汉字和ascii混合时如何控制输出格式?

时间:2014-04-27 06:27:03

标签: python python-3.x

我发现很难使文字对齐。

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

不是每一行都是(12 + 2 + 10)* 2 + 4字符宽度,相同的格式控制语句,但输出不同,我如何修复它以使每一行具有相同的输出格式,相同的宽度?

2 个答案:

答案 0 :(得分:5)

您可以考虑字符串中的全宽与半宽字符数:

#!/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]))

输出:

enter image description here

答案 1 :(得分:1)

不漂亮,但这是单向的。 (用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'))

输出:

enter image description here