python3 输出中文报错,代码应该没问题,感觉是 cmd 编码的问题,但是百度了好久还是无法解决

2014-03-17 12:35:14 +08:00
 rikka
# -- coding: utf-8 --
a='大小写'
print(a)
=========
使用idel能正常输出
但是运行python -u d:\a.py 就报这个错误
======================
Traceback (most recent call last):
File "D:\a.py", line 5, in <module>
print(a)
File "C:\Python30\lib\io.py", line 1491, in write
b = encoder.encode(s)
File "C:\Python30\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
24941 次点击
所在节点    Python
57 条回复
intosec
2014-03-17 15:37:17 +08:00
是有这个问题,sqlmap在windows的cmd下也无法显示中文字符,改了cmd的字符集也没有用,当时想了个折衷的办法,就是用cygwin运行sqlmap。
rikka
2014-03-17 15:38:49 +08:00
右下角显示ansi as utf-8
没错啊
josephshen
2014-03-17 15:44:04 +08:00
来,用notpad++把编码转换成 utf-8,注意有BOM。
rikka
2014-03-17 15:45:18 +08:00
@josephshen
把编码调来调去,最后还是调成utf8,但是报错变成
a='\u5927\u5c0f\u5199
^
SyntaxError: EOL while scanning string literal
josephshen
2014-03-17 15:45:28 +08:00
再跑一遍,告诉我你发现哪里有不同了
josephshen
2014-03-17 15:48:26 +08:00
好,告诉我,你现在觉得哪里出问题了
rikka
2014-03-17 15:49:25 +08:00
@josephshen
哦,不是
a='\u5927\u5c0f\u5199
^
SyntaxError: EOL while scanning string literal
这个报错是原因我写漏了一个引号
然后无论是 utf-8有BOM还是无BOM
报错信息跟一开始还是一样
josephshen
2014-03-17 15:53:10 +08:00
position 90??这个数字,变了没有??
rikka
2014-03-17 15:56:37 +08:00
@josephshen
咦,这里有变化我没注意到
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
josephshen
2014-03-17 16:01:18 +08:00
刚上了厕所,
好,你现在告诉我,你觉得哪块出错了?
rikka
2014-03-17 16:01:23 +08:00
发现在开头加上
print(repr(open(__file__).read()))
这句position有变化
现在代码是
# -- coding: utf-8 --
import sys
import os
print(repr(open(__file__).read()))
a='大小写'
print(a)
# os.system("pause")
报错是
Traceback (most recent call last):
File "D:\a.py", line 4, in <module>
print(repr(open(__file__).read()))
File "C:\Python30\lib\io.py", line 1724, in read
decoder.decode(self.buffer.read(), final=True))
File "C:\Python30\lib\io.py", line 1295, in decode
output = self.decoder.decode(input, final=final)
File "C:\Python30\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 93: character maps to <undefined>
rikka
2014-03-17 16:04:11 +08:00
还是看不出哪里错了T_T
josephshen
2014-03-17 16:13:26 +08:00
好,你发现是print(repr(open(__file__).read()))有问题是吧?
josephshen
2014-03-17 16:14:43 +08:00
这句里面有很多条代码,你一个个展开,每一句分开写,再跑一遍,告诉我哪错了
xiaket
2014-03-17 16:21:51 +08:00
Windows下要玩python开个Linux虚拟机吧, 别折腾自己了.
rikka
2014-03-17 16:22:45 +08:00
@josephshen
代码是
# -- coding: utf-8 --
import sys
import os
q=open(__file__).read()
w=repr(q)
print(w)
a='大小写'
print(a)
# os.system("pause")
=======
报错是
Traceback (most recent call last):
File "D:\a.py", line 4, in <module>
q=open(__file__).read()
File "C:\Python30\lib\io.py", line 1724, in read
decoder.decode(self.buffer.read(), final=True))
File "C:\Python30\lib\io.py", line 1295, in decode
output = self.decoder.decode(input, final=final)
File "C:\Python30\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 102: character maps to <undefined>
======
好像是open(__file__).read()这里错了,因为文件中有中文所以出了问题
josephshen
2014-03-17 16:27:59 +08:00
好,你现在找到了原因,解决办法另行Google。
gamexg
2014-03-17 16:33:30 +08:00
# -- coding: utf-8 --
import sys
a='大小写'
print(a)
print(sys.stdout.encoding)
print(sys.getdefaultencoding())
print(sys.getfilesystemencoding())


D:\>c:\Python33\python.exe t.py
大小写
cp936
utf-8
mbcs

D:\>chcp
活动代码页: 936

D:\>c:\Python33\python.exe
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

我这里没有这个问题啊,奇怪的事情。

还有open这样用应该不会报错了
open(r"d:\111.txt",'w',encoding='utf8')
rikka
2014-03-17 16:36:24 +08:00
@josephshen 不是吧,TAT
rikka
2014-03-17 16:37:03 +08:00
@gamexg
print(sys.stdout.encoding)
print(sys.getdefaultencoding())
print(sys.getfilesystemencoding())
我的结果是
cp1252
utf-8
mbcs

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/104648

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX