繁体   English   中英

在Bash shell中,为什么Python只会在重定向或管道输出时抱怨打印非ascii字符?

[英]In Bash shell, why does Python complain about printing non-ascii characters only when the output is redirected or piped?

打印终端:

> python -c 'print u"\xae"' # no complaints here!
®

打印到文件:

> python -c 'print u"\xae"' > tmp.txt
Traceback (most recent call last):
  File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 0: ordinal not in range(128)

例如,当管道连接到cat ,也会发生错误。

因为它无法正确猜测编码并假设它是“ascii”。 当输出不是TTY时,编码为UTF-8。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM