這裏是我的代碼:的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