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: