Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当汉字和ascii码混合时,如何控制输出格式?_Python_Python 3.x - Fatal编程技术网

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

Python 当汉字和ascii码混合时,如何控制输出格式?,python,python-3.x,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/0

我发现很难使文本对齐

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


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

不漂亮,但这里有一种方法。(使用
字节编码/解码。rjust

输出:


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

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


实际上,你不能。你知道为什么Python3的字符串格式不能像预期的那样开箱即用吗?我希望它能做到这一点,但也许这是幼稚的?我已经尝试过了,这种方法可以正确地处理希腊字符。行
提江·阿布拉,男,1979/07/10
源代码中缺少首字母
买买@根据文献,您可以看到歧义字符的显示取决于字体/终端。有时它们很宽,但在上面的
“·”
字符打印得很窄。在GB18030中编码为2字节,因此打印输出缺少一个焊盘空间,无法在表中正确排列数据[8][0]。这种方法只有在ASCII码和汉字混合时才可靠,但这正是您所要求的。有两种方法:1.@falsetry方法,当我添加买买 在第8行,在另一行添加希腊字符,可以正确显示@抢劫ᵩ ,将返回和(unicodedata.east\u asian\u width(x)='W'表示s中的x)修改为返回和(unicodedata.east\u asian\u width(x)='W'或unicodedata.east\u asian\u width(x)='A'表示s中的x),否则无法正确显示歧义字符。将返回和(Unicoded Data.east\u asian\u width(x)='W'表示s中的x)修改为
返回和(unicodedata.east_asian_width(x)='W'或unicodedata.east_asian_width(x)='A'表示s中的x)
可以在控制台中获得更好的显示效果。
#!/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]))