宽东亚字符与格式功能的对齐

时间:2014-02-20 23:19:30

标签: python unicode

我正在使用python format方法在我的终端显示中对齐East Asian Wide (EAW) characters。以下python3代码说明了我的问题。

ZH_STRING1 = "東京大学"
ZH_STRING2 = "麻将"

print(">" + "01234567" + "<")
print(">" + format(ZH_STRING1, ">4") + "<")
print(">" + format(ZH_STRING2, ">4") + "<")
print("---")
print("Length:         ", len(ZH_STRING2))

终端输出(见下图)显示EAW字符宽度是普通字符宽度的两倍,而len函数返回正确的字符数(每个表意符号一个)。

Terminal output of bad east asian wide characters aligment.

python format函数将ZH_STRING1长度评估为4,将ZH_STRING2长度评估为2,format函数添加两个空格字符以进行对齐。不幸的是,EAW和空格字符之间的宽度不同会影响终端输出。

那么,我怎样才能找到正确的对象?

1 个答案:

答案 0 :(得分:2)

事实上,我在写这个问题时找到了解决方案。 ^^

这很简单:在python format函数中使用Unicode表意空间(U + 3000),该函数占用终端输出上的正常空格:

ZH_STRING1 = "東京大学"
ZH_STRING2 = "麻将"

print(">" + "01234567" + "<")
print(">" + format(ZH_STRING1, " >4") + "<")
print(">" + format(ZH_STRING2, " >4") + "<")

然后:

enter image description here