The ultimate solution for reading and writing files in Python unicodedecodeerror: ' GBK ' codec can ' t decode byte 0x9d in position 1270:illega

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.