Python UnicodeEncodeError: 'gbk' codec can't encode character solution, pythonencode

Source: Internet
Author: User

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.

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.