Python UnicodeEncodeError: 'gbk' codec can't encode character solution, pythonencode
When using Python to write files or writing network data streams to local files, the following error occurs in most cases: UnicodeEncodeError: 'gbk' codec can't encode character '\ xa0' in position... this problem. There are many similar files on the network that tell you how to solve this problem, but it is nothing more than encode and decode. Is this the real cause of this problem? No. Most of the time, we use decode and encode, and tried various encodings, such as utf8, UTF-8, gbk, and gb2312. All the encodings were tried, but they still appeared during compilation: unicodeEncodeError: 'gbk' codec can't encode character '\ xa0' in position XXX. Crashed.
Compiling a python script in windows causes serious Encoding Problems.
When writing network data streams to files, we will encounter several encodings:
1: # encoding = 'xxx' here (that is, the first line of the python file) encoding refers to the encoding of the python script file itself, irrelevant. As long as XXX is the same as the file encoding. For example, you can set various encodings In the notepad ++ "format" menu. You must ensure that the encoding set in this menu is the same as encoding XXX. If the encoding is different, an error is returned.
2: The encoding of the network data stream, such as obtaining a webpage, indicates the encoding of the network data stream. Decode must be decoded to unicode encoding.
3: If the encoding of the target file is to write the encoding of the network data stream to the new file, do I need to specify the encoding of the new file. The code for writing files is as follows:
Copy codeThe Code is as follows:
F. write (txt)
. Txt is a string that has been decoded by decode. The key point is: the encoding of the target file is the culprit of the title. If we open a file:
Copy codeThe Code is as follows:
F = open ("out.html", "w ")
In windows, the default encoding of the new file is gbk. In this case, the python interpreter will use gbk encoding to parse our network data stream txt, however, txt is now a decode unicode code. In this case, the parsing will fail and the above problems will occur. The solution is to change the encoding of the target file:
Copy codeThe Code is as follows:
F = open ("out.html", "w", encoding = 'utf-8 ')
. In this way, the problem no longer exists.