【发布时间】:2018-08-05 15:44:59
【问题描述】:
我注意到,如果没有源代码编码声明,Python 2 解释器假定源代码是用 scripts 和 standard input 以 ASCII 编码的:
$ python test.py # where test.py holds the line: print u'é'
File "test.py", line 1
SyntaxError: Non-ASCII character '\xc3' in file test.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
$ echo "print u'é'" | python
File "/dev/fd/63", line 1
SyntaxError: Non-ASCII character '\xc3' in file /dev/fd/63 on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
它以 ISO-8859-1 编码,带有 -m module 和 -c command 标志:
$ python -m test # where test.py holds the line: print u'é'
é
$ python -c "print u'é'"
é
记录在哪里?
与 Python 3 相比,Python 3 始终假定源代码以 UTF-8 编码,因此在四种情况下打印 é。
注意。 – 我在 macOS 10.13 和 Ubuntu Linux 17.10 上的 CPython 2.7.14 上对此进行了测试,控制台编码设置为 UTF-8。
【问题讨论】:
标签: python character-encoding ascii iso-8859-1 python-internals