讀文件時出現這個錯誤 'utf-8' codec can't decode byte 0xba in position 21: invalid start byte


''' 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



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM