UnicodeDecodeError: 'gbk' codec can't decode byte 0xb0 in position 279: illegal multibyte sequence


with open(r'E:\yy\mysql.txt') as wk:
    print(wk.readlines())
Traceback (most recent call last):
  File "C:\eclipse-workspace\py3_test\test\test3.py", line 8, in <module>
    print(wk.readlines())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb0 in position 279: illegal multibyte sequence

讀取文件時,報錯,解決方案:

with open(r'E:\yy\mysql.txt',encoding='utf-8') as wk:
    print(wk.readline())

這種方法讀取的內容是字符串類型

<class 'str'>

with open(r'E:\yy\mysql.txt','rb') as wk:
    print(wk.readline())

這種方法讀取的內容是字節類型

<class 'bytes'>

 

關於讀取文件,首行會多 ‘\ufeff’ 的處理方法:https://www.cnblogs.com/chongzi1990/p/8694883.html

with open(r'E:\yy\mysql.txt',encoding='UTF-8-sig') as wk:

 


免責聲明!

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



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