【发布时间】:2015-10-15 05:15:54
【问题描述】:
在 Python 中,很容易使用 string.format 填充(即填充)字符串并将它们向左、向右或居中对齐。例如:
>>> word = "Resume"
>>> print "123456890\n{0:>{1}}".format(word, 10)
>>> print len(name)
1234567890
Resume
6
但是,如果字符串包含多字节 Unicode 字符,string.format 不会正确计算字符串的宽度:
>>> word = u"Résumé"
>>> print "123456890\n{0:>{1}}".format(word.encode('utf8'), 10)
>>> print len(name.encode('utf8'))
1234567890
Résumé
8
解决方案不使用unicodedata.normalize('NFC', string),您可能已经阅读过。这确实将normalize Unicode character 序列(在某些情况下也可能是必要的!)但它确实不会导致string.format 正确计算字符串的编码宽度以输出到终端。 p>
那么如何在 Python 2.7 中使用string.format 打印正确填充/填充的字符串?
【问题讨论】:
标签: python string python-2.7 unicode