V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  rikka  ›  全部回复第 10 页 / 共 12 页
回复总数  228
1 ... 2  3  4  5  6  7  8  9  10  11 ... 12  
@gamexg
print(sys.stdout.encoding)
print(sys.getdefaultencoding())
print(sys.getfilesystemencoding())
我的结果是
cp1252
utf-8
mbcs
@josephshen 不是吧,TAT
@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()这里错了,因为文件中有中文所以出了问题
还是看不出哪里错了T_T
发现在开头加上
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>
@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
哦,不是
a='\u5927\u5c0f\u5199
^
SyntaxError: EOL while scanning string literal
这个报错是原因我写漏了一个引号
然后无论是 utf-8有BOM还是无BOM
报错信息跟一开始还是一样
@josephshen
把编码调来调去,最后还是调成utf8,但是报错变成
a='\u5927\u5c0f\u5199
^
SyntaxError: EOL while scanning string literal
右下角显示ansi as utf-8
没错啊
不过奇怪我系统是win7 64位,python也是下载64位的,这里怎么显示on win32
@josephshen
Python 3.0 (r30:67507, Dec 3 2008, 19:44:23) [MSC v.1500 64 bit (AMD64)] on win32
@josephshen FaceName是新宋体
@est
代码是这样的# -- 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 90: character maps to <undefined>
=====
去掉
a='大小写'
print(a)
正常
'# -- coding: utf-8 --\nimport sys\nimport os\nprint(repr(open(__file__).read()))\n\n# os.system("pause") \n'
@josephshen
注册表改成936执行chcp结果是437
注册表改成65001执行chcp结果是65001
@josephshen
改完之后变成进入python交互模式无论输入任何语句或者错误不存在的函数都没报错也没显示什么东西
改成963
输入print('大小写')
显示???
@VYSE
python3的sys模块已经移除了setdefaultencoding
sys.stdout.encoding是cp1252
=====
# -- coding: utf-8 --
import sys
PYTHONIOENCODING="utf-8"
print(sys.stdout.encoding)
a='大小写'
print(a)
=====
也是报同样的错
@sobigfish 确认没有BOM
@tommark 确认是utf-8
1 ... 2  3  4  5  6  7  8  9  10  11 ... 12  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2461 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 20ms · UTC 08:22 · PVG 16:22 · LAX 01:22 · JFK 04:22
Developed with CodeLauncher
♥ Do have faith in what you're doing.