|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
>>> f = open('C:\\Users\\83489\\Desktop\\新建文本文档.txt')
>>> f.read()
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 40: illegal multibyte sequence
修改后
>>> f = open('C:\\Users\\83489\\Desktop\\新建文本文档.txt',encoding = 'utf8')
>>> f.seek(5)
5
>>> f.readline()
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
f.readline()
File "C:\Users\83489\AppData\Local\Programs\Python\Python38\lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 0: invalid start byte
可以read,但是readline报错
想知道为什么?
本帖最后由 sunrise085 于 2020-3-15 21:20 编辑
这个错误是说utf-8编码格式下,不能seek(5),会导致出错。
原因是utf-8编码格式下,每个汉字占位3字节,你使用f.seek(5)后,光标不在正常的汉字开始处,读取出错了。
|
|