通过Python

时间:2018-06-11 15:17:05

标签: python string utf-8

尝试使用简单的open ()方法使用Python 3.6打开.txt文件(其中只包含纯文本)时出现问题:

with open('3003.txt', 'r') as myfile:
    data=myfile.read()

它给出了这样的错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe0 in position 182: invalid continuation byte

我使用了几种googled方法,但他们没有解决这个问题。应该怎么做来解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以使用Python 3 open()样式文件处理程序来处理字节串:

open('3003.txt', 'rb') as myfile:
    data=myfile.read()

请注意' b'意思是二元模式