UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 309: illegal multibyte sequence
Two solutions.
Recently, the next problem encountered in the operation of txt file market is:
UnicodeDecodeError:'gbk' codec can't decode byte 0x80 in position 309: illegal multibyte sequence
as shown in the figure
. There are two solutions: the
first method is recommended: add one afterwards , encoding='UTF-8'.
Just change open(path,"r") to open(path,"r",encoding='UTF-8')
data=[]
for line in open("D:\\3\\202010161823\\recode.log.txt","r",encoding='UTF-8'): #设置文件对象
data.append(line[:-1])
The second method: Change "r" to "rb". But this method is binary. Everyone look at the selection
data=[]
for line in open("D:\\3\\202010161823\\recode.log.txt","rb"): #设置文件对象
data.append(line[:-1])
This is a personal note. First of all, you can refer to the following article about reading and writing txt with python.
"Common read and write operations of python txt files": https://www.cnblogs.com/youyou0/p/8921719.html
"Python reads and writes txt text files": https://www.cnblogs.com/hackpig/p/8215786.html
"Python reads and writes txt text content":
https://blog.csdn.net/qq_37828488/ article/details/100024924
Concluding remarks
Like and follow if you have any help. Thank you all.