如何解决“ UnicodeDecodeError:'ascii'编解码器无法解码字节”

时间:2018-09-12 00:06:13

标签: python

我正在编写一个程序,用于计算文件中的大约单词数,并得到一个错误,指出'ascii' codec can't decode byte

如何消除此错误?

以下是上述错误的回溯:

Traceback (most recent call last):
  File "/Users/NikolaMac/Desktop/alice.py", line 23, in <module>
    contents = f_obj.read()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)"

这是我的代码:

filename='alice.txt'

try:
    with open(filename) as f_obj:
        contents = f_obj.read()

except FileNotFoundError:
    msg = "Sorry, the file " + filename + " does not exist."
    print(msg)

else:
    # Count the approximate number of words in the file.
    words = contents.split()
    num_words = len(words)
    print("The file " + filename + " has about " + str(num_words) + " words.")

2 个答案:

答案 0 :(得分:2)

您需要改用io.open函数,并将其传递给编码。

尝试一下:

import io

with io.open(filename, encoding='utf-8') as f_obj:
    contents = f_obj.read()

print('Words: %d'%len(contents.split(' ')))

答案 1 :(得分:0)

该错误消息表明它尝试使用ASCII解码。您可能需要指定其他编码。

我能看到的唯一程序代码是open调用。根据{{​​3}},如果您未明确传递编码,

  

默认编码取决于平台(无论locale.getpreferredencoding()返回什么)

尝试将encoding='utf-8'传递给open呼叫。