Unicodedecodeerror: ' GBK ' codec can ' t decode byte 0x9d in position 1270:illegal multibyte sequence
Above is the error encountered, would like to complete the read file, and then write another file. But in the Fp.read (), has encountered the above error, after a variety of Baidu, Google, and the magic of StackOverflow only to know that the character stream problem.
Know the problem, or do not solve, and hard search, finally found inspiration in StackOverflow, you can turn the open way into binary, that is, the following code in the open (filename, ' RB '), this good, at least the back of the read () can pass.
After that, the following code is generated, and the road to finding the problem is really not good to go, under this mark.
# _*_coding:utf-8_*_import‘section5‘fp = open(filename,‘rb‘)content = fp.read().decode(‘utf-8‘r‘<p>(.*?)</p>‘#print(content)result = re.findall(reg,content)fp.close()fp = open(filename+‘.txt‘,‘bw‘)forin result: r = r.encode(‘utf-8‘) fp.write(r) fp.write(‘\n‘.encode(‘utf-8‘))fp.close()
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The ultimate solution for reading and writing files in Python unicodedecodeerror: ' GBK ' codec can ' t decode bytes 0x9d in position 1270:illega