Python3 Text Encoding error: unicodedecodeerror: ' GBK ' codec can ' t decode byte

Source: Internet
Author: User

An error message occurred while reading a file using Python3

Unicodedecodeerror: ' GBK ' codec can ' t decode byte

This is due to Python in the decoding process error, all character encoding in Python3 is Unicode encoding, and in the file to be read in Chinese, which is beyond the GBK encoding representation range, GBK encoding can not decode it, so error.

After some Baidu, find the following solutions:

1. Set the encoding when opening the file, such as: Open (' 1.txt ', encoding= ' utf-8 ')

2, if there is more than GBK code to indicate the range of characters, you can choose to encode a wider range of ' gb18030 ', open (' 1.txt ', encoding= ' GB18030 ')

3, the text appears even ' GB18030 ' also cannot encode character, can use ' ignore ' attribute to ignore, open (' 1.txt ', encoding= ' gb18030 ', errors= ' ignore ')

4. There is also a common solution for open (' 1.txt '). Read (). Decode (' gb18030 ', ' ignore ')

5, you can turn the open way into binary, open (filename, ' RB ')

Python3 Text Encoding error: unicodedecodeerror: ' GBK ' codec can ' t decode byte

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.