ascii编解码器无法解码

时间:2016-06-03 07:09:46

标签: list python-3.x encoding ascii utf

我在尝试打开目录中的所有.txt文件时遇到错误,当我的代码中只有1个txt文件时,我的代码会起作用,否则会弹出此消息:

Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.run "my.exe --a=20 --b=NORMAL < file.txt"

这是我的代码:

Traceback (most recent call last):
  File "/Users/Sierra/Desktop/TCSS 142/Project 2/syanh7_project2.py", line 17, in <module>
for line in file:
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py", line 26, in decode
   return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0x92 in position 3: ordinal not in range(128)

2 个答案:

答案 0 :(得分:0)

好的,所以@Evert的评论解决了我的问题,当我打开文件时,所以我的开放语句看起来像这样:

file = open(el, 'r', encoding = 'cp1252')

答案 1 :(得分:0)

您的默认编码(用于读取文件)似乎是ASCII;它无法读取的字符似乎是(Windows)智能引用(&#34;卷曲&#34;撇号')。

为了能够读取文件,您需要指定其编码。我不认为0x92是一个有效的UTF代码点(但我可能会弄错);由于它是Windows智能引用,请尝试Windows latin alphabet encoding cp1252

file = open(el, 'r', encoding = 'cp1252')