阿啦CP 发表于 2022-3-7 11:22:15

怎么解决这个error

file = open('/Users/yedaoguang/Desktop/lessonpy/OpenMe.mp3')
for each_line in file:
    print(each_line, end='')

file.close()运行出错显示
Traceback (most recent call last):
File "/Users/yedaoguang/Desktop/lessonpy/readfile.py", line 2, in <module>
    for each_line in file:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte
这个我应该怎么改code

梁华峰 发表于 2022-3-7 11:23:03

"OpenMe.mp3" 是个文本文件,之所以出错是因为其中包含有中文字符。

    改成下面这样就可以了:

file = open('/Users/yedaoguang/Desktop/lessonpy/OpenMe.mp3' , "rt" , encoding = "cp936")
for each_line in file:
    print(each_line, end='')

file.close()

水煮沉浮 发表于 2022-3-7 11:23:16


用记事本打开 openme.mp3 另存为,然后选编码为ansi保存
页: [1]
查看完整版本: 怎么解决这个error