Using Unicode string literals as Nam suggested is correct, but if your terminal is using the cp437 code page as your output suggests, it will not be able to display some of the Unicode characters you want to use. The Windows console doesn't support UTF-8, which is what you are sending if you declare coding: utf-8
1 in your source file and do not use Unicode literals. coding: utf-8
declares the encoding of your source file, so make sure you are actually saving your source in UTF-8 encoding.
当您使用 Unicode 文字时,Python 会解释声明的编码中的源字符串,并将其转换为 Unicode 字符串。当打印 Unicode 字符串时,Python 会使用终端编码对字符串进行编码,或者缺少终端编码,则使用默认编码ascii
对于Python 2。
一个例子:
# coding: utf8
print '£9.2 billion' # Sends UTF-8 to cp437 terminal (gibberish)
print u'£9.2 billion' # Correctly prints on cp437 terminal.
print 'Sheffield’s' # Sends UTF-8 to cp437 terminal (gibberish)
# Replaces Unicode characters that are unsupported in cp437.
print u'Sheffield’s £9.2 billion'.encode('cp437','xmlcharrefreplace')
print u'Sheffield’s' # UnicodeEncodeError.
Output
£9.2 billion
£9.2 billion
SheffieldΓÇÖs
Sheffield’s £9.2 billion
Traceback (most recent call last):
File "C:\Documents and Settings\metolone\Desktop\x.py", line 10, in <module>
print u'SheffieldΓÇÖs' # UnicodeEncodeError.
File "C:\dev\python27\lib\encodings\cp437.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u2019' in position 9: character maps to <undefined>
因此,不要指望能够在 Windows 控制台上正确打印所有 Unicode。使用支持 UTF-8 的 Python IDE,例如 PythonWin(可在pywin32 http://sourceforge.net/projects/pywin32/files/pywin32扩大)。
要在 Windows 控制台中正确显示 Unicode 字符,您需要两件事:映射要显示的 Unicode 字符的编码,以及支持这些字符的正确字形的字体。对于您的示例,如果将控制台代码页更改为 Windows-1252 (chcp 1252
)并将控制台字体更改为 Consolas 或 Lucida Console 而不是光栅字体,如果您使用 Unicode 文字(p = u"..."
).