1

我正在尝试将 UTF-8 编码的字符串与string.ljust. 引发此异常:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128). 例如,

s = u"你好"    // a Chinese string
stdout.write(s.encode("UTF-8").ljust(20))

我在正确的轨道上吗?或者我应该使用其他方法来格式化?

谢谢和最好的问候。

4

1 回答 1

5

您是否发布了确切的代码和收到的确切错误?cp437因为您的代码可以正常工作而不会在 a和utf-8终端上引发错误。在任何情况下,您都应该在将 Unicode 字符串发送到终端之前证明其合理性。请注意区别,因为 UTF-8 编码的中文在编码时的长度为 6 而不是长度为 2:

>>> sys.stdout.write(s.encode('utf-8').ljust(20) + "hello")
你好              hello
>>> sys.stdout.write(s.ljust(20).encode('utf-8') + "hello")
你好                  hello

另请注意,中文字符比典型的固定宽度字体中的其他字符更宽,因此如果混合语言,事情可能仍然无法按照您的意愿排列(请参阅此答案以获得解决方案):

>>> sys.stdout.write("12".ljust(20) + "hello")
12                  hello

通常你可以跳过显式编码到stdout. Python 以终端的编码方式将 Unicode 字符串隐式编码到终端(请参阅 参考资料sys.stdout.encoding):

sys.stdout.write(s.ljust(20))

另一种选择是使用print

print "%20s" % s   # old-style

或者:

print '{:20}'.format(s)  # new-style
于 2012-06-07T06:08:14.610 回答