这里是我的代码:的Python:从文件显示错误Ascii字符
import sys, os
print("█████") #<-- Those are solid blocks.
f= open('file.txt')
for line in f:
print(line)
在file.txt的是这样的:
hay hay, guys
████████████
但输出是这样的:
██████
hay hay, guys <----- ***Looks like it outptutted this correctly!***
Traceback (most recent call last):
File "echofile.py", line 6, in <module>
print(line)
File "C:\python33\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 1-2: cha
racter maps to <undefined> <------ ***But not from the file!***
任何人有什么建议,为什么它这样做?我在IDLE中编写代码,尝试在Programmer的记事本和IDLE中编辑file.txt。该文件是ASCII/ANSI。顺便说一句,我使用的是Python 3。 3.3 alpha-win-64如果它很重要。
CP437输出该: █▓▓▓▓▓▓█<---印刷 干草干草,球员 asdfΓûôΓûôΓûô Γûê Γûê< - 从文件打印。 – SuperDisk 2012-04-18 21:36:31
在这种情况下,我会猜测cp437是不正确的。 UTF-8是默认的,这也是不正确的。我不知道该告诉你什么;你需要弄明白。 – steveha 2012-04-18 21:38:12
实际上,只是将编码指定为'utf-8',但不指定编码。奇怪的。 – SuperDisk 2012-04-18 21:46:14