您是否发布了确切的代码和收到的确切错误?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