It is possible to use encodings different than ASCII in Python source
files. The best way to do it is to put one more special comment line
right after the #!
line to define the source file encoding:
Python 的源文件可以通过编码使用 ASCII 以外的字符集。
最好的做法是在 #!
行后面用一个特殊的注释行来定义字符集。
# -*- coding: iso-8859-1 -*-
With that declaration, all characters in the source file will be treated as
iso-8859-1
, and it will be
possible to directly write Unicode string literals in the selected
encoding. The list of possible encodings can be found in the
Python Library Reference, in the section
on codecs.
根据这个声明,Python 会将文件中的字符尽可能的从指定的编码转为 Unicode,在本例中,这个字符集是 iso-8859-1 。在 Python 库参考手册 中 codecs部份可以找到可用的编码列表(根据个人经验,推荐使用 cp-936或utf-8处理中文--译者注)。
If your editor supports saving files as UTF-8
with a UTF-8
byte order mark (aka BOM), you can use that instead of an
encoding declaration. IDLE supports this capability if
Options/General/Default Source Encoding/UTF-8
is set. Notice
that this signature is not understood in older Python releases (2.2
and earlier), and also not understood by the operating system for
#!
files.
如果你的文件编辑器支持 UTF-8
格式,并且可以保存
UTF-8
标记(aka BOM - Byte Order
Mark),你可以用这个来代替编码声明。IDLE可以通过设定Options/General/Default
Source Encoding/UTF-8
来支持它。需要注意的是旧版Python不支持这个标记(Python
2.2或更早的版本),也同样不能使操作系统支持#!
文件。
By using UTF-8 (either through the signature or an encoding declaration), characters of most languages in the world can be used simultaneously in string literals and comments. Using non-ASCII characters in identifiers is not supported. To display all these characters properly, your editor must recognize that the file is UTF-8, and it must use a font that supports all the characters in the file.
使用 UTF-8 内码(无论是用标记还是编码声明),我们可以在字符串和注释中使用世界上的大部分语言。标识符中不能使用非 ASCII 字符集。为了正确显示所有的字符,你一定要在编辑器中将文件保存为 UTF-8 格式,而且要使用支持文件中所有字符的字体。