读取.txt文件时,macthon中的UnicodeDecodeError与python3

时间:2018-06-01 20:50:05

标签: python python-3.x

我在mac中用python3.6写了一些文件.txt。然后,当我尝试使用以下内容阅读它们时:

nl2br()

我收到了这个错误:

  

UnicodeDecodeError:' utf-8'编解码器不能解码位置10中的字节0xe8:无效的连续字节

有人可以帮忙吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

如何在python中创建文件?你能否展示你的代码的完整例子?我也在Mac上工作,但没有问题。请参阅以下有关其工作原理的简单示例之一:

创建文件:

file = open('test9.txt', 'w')
file.write('first \n second line \n aaa')
file.close()

读取文件:

with open('test9.txt') as f:
    lines = f.readlines()
    print(lines)

创建的文件是带有.txt

的纯文本文档