设为首页 加入收藏

TOP

读文件时出现这个错误 'utf-8' codec can't decode byte 0xba in position 21: invalid start byte
2019-07-08 22:10:25 】 浏览:247
Tags:文件 出现 这个 错误 ' utf-8' codec can' decode byte 0xba position 21: invalid start

''' file2 文件内容:


很任性
when
i
was
young

'''


源代码:
f = open("file2",'r',encoding="utf-8")
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())


错误提示:
D:\python\python.exe D:/pythonWork/day2/file-op.py
Traceback (most recent call last):
  File "D:/pythonWork/day2/file-op.py", line 10, in <module>
    print(f.readline())
  File "D:\python\lib\codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 21: invalid start byte

修改之后代码:
f = open("file2",'r',encoding="gbk")
print(f.readline())    
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())


执行结果:

很任性
when
i
was
young

Process finished with exit code 0


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇学习第三天:总结 下一篇整型,布尔值以及字符串详解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目