当我尝试在Windows控制台中打印Unicode字符串时,出现UnicodeEncodeError: 'charmap' codec can't encode character ....错误.我认为这是因为Windows控制台不接受仅Unicode字符.最好的方法是什么??在这种情况下,有什么方法可以让Python自动打印而不是失败?
编辑: 我正在使用Python 2.5.
注意: @ LasseV.Karlsen回答带有复选标记有点过时(从2008年开始).请谨慎使用下面的解决方案/答案/建议!!
截至今天(2016年1月6日),@ JFSebastian答案更为相关.
我正在阅读和解析Amazon XML文件,而XML文件显示',当我尝试打印它时,我收到以下错误:
'ascii' codec can't encode character u'\u2019' in position 16: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
从我到目前为止在线阅读的内容来看,错误来自于XML文件是UTF-8,但Python希望将其作为ASCII编码字符处理.是否有一种简单的方法可以使错误消失并让我的程序在读取时打印XML?
如何print("Some text")在UTF-8中使用python 3(3.1)到stdout,或者如何输出原始字节?
TestText = "Test - ??????..šŠ??žŽ" # this is UTF-8
TestText2 = b"Test2 - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd" # just bytes
print(sys.getdefaultencoding())
print(sys.stdout.encoding)
print(TestText)
print(TestText.encode("utf8"))
print(TestText.encode("cp1252","replace"))
print(TestText2)
Run Code Online (Sandbox Code Playgroud)
输出(在CP1257和I中将字符替换为字节值[x00]):
utf-8
cp1257
Test - [xE2][xC2][xE7][C7][xE8][xC8]..[xF0][xD0][xFB][xDB][xFE][xDE]
b'Test - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd'
b'Test - ??????..\x9a\x8a??\x9e\x8e'
b'Test2 - \xc4\x81\xc4\x80\xc4\x93\xc4\x92\xc4\x8d\xc4\x8c..\xc5\xa1\xc5\xa0\xc5\xab\xc5\xaa\xc5\xbe\xc5\xbd'
Run Code Online (Sandbox Code Playgroud)
print太聪明了......:D使用编码文本是没有意义的print(因为它总是只显示字节的表示而不是实际字节)并且根本不可能输出字节,因为无论如何打印并始终对其进行编码sys.stdout.encoding.
例如:print(chr(255))抛出错误:
Run Code Online (Sandbox Code Playgroud)Traceback (most recent call last): File "Test.py", line 1, in <module> print(chr(255)); File "H:\Python31\lib\encodings\cp1257.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode …
我正在制作纸牌游戏,但我遇到了似乎是一个编码问题.我正在尝试打印这样一张卡片:
def print(self):
print("|-------|")
print("| %s |" % self.value)
print("| |")
print("| %s |" % self.suit.encode("utf-8"))
print("| |")
print("| %s |" % self.value)
print("|-------|")
Run Code Online (Sandbox Code Playgroud)
这就是我要的:
|-------|
| 10 |
| |
| ? |
| |
| 10 |
|-------|
Run Code Online (Sandbox Code Playgroud)
......但这就是我得到的:
|-------|
| 10 |
| |
| b'\xe2\x99\xa6' |
| |
| 10 |
|-------|
Run Code Online (Sandbox Code Playgroud)
如果重要的话,我在Windows和Python 3上.
self.suit的值可以是以下任何一个:
spade = "?"
heart = "?"
diamond = "?"
club = "?"
Run Code Online (Sandbox Code Playgroud)
如果我删除.encode("utf-8"),我会收到此错误:
Traceback(最近一次调用最后一次):
File "main.py", line 79, in <module>
start() …Run Code Online (Sandbox Code Playgroud) 我正在开发一个python应用程序,可以在多个平台上以多种语言将文本打印到控制台.该程序适用于所有UNIX平台,但在Windows中,在命令行中打印unicode字符串时出错.
已经有一个相关的线程:( Windows cmd编码更改导致Python崩溃)但我找不到我的具体答案.
例如,对于以下亚洲文本,在Linux中,我可以运行:
>>> print u"\u5f15\u8d77\u7684\u6216".encode("utf-8")
????
Run Code Online (Sandbox Code Playgroud)
但在Windows中,我得到:
>>> print u"\u5f15\u8d77\u7684\u6216".encode("utf-8")
?????????µ??
Run Code Online (Sandbox Code Playgroud)
在做类似的事情时,我成功地用消息框显示正确的文本:
>>> file("bla.vbs", "w").write(u'MsgBox "\u5f15\u8d77\u7684\u6216", 4, "MyTitle"'.encode("utf-16"))
>>> os.system("cscript //U //NoLogo bla.vbs")
Run Code Online (Sandbox Code Playgroud)
但是,我希望能够在Windows控制台中完成它,并且最好 - 在我的python代码之外不需要太多配置(因为我的应用程序将分发给许多主机).
这可能吗?
编辑:如果不可能 - 我很乐意接受在Windows中显示unicode编写控制台应用程序的其他一些建议,例如另一个Windows控制台的python实现