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