[Python] Unicodeencodeerror: ' GBK ' codec can ' t encode character solution

Source: Internet
Author: User
Tags python script

Writing Python scripts under Windows is a serious coding problem.

When you write a network data stream to a file, we encounter several encodings:

1: #encoding = ' XXX ' here (i.e. the contents of the first line of the Python file) refers to the encoding of the Python script file itself, which does not matter. As long as xxx and the file itself is the same code. For example, notepad++ "format" menu can be set up a variety of codes, it is necessary to ensure that the menu set encoding and encoding xxx the same line, different words will be error

2: Network data stream encoding such as access to Web pages, then the network data stream encoding is the page encoding. The Unicode encoding needs to be decoded using decode.

3: Target file Encoding to write the encoding of the network data stream to the new file, then I need to specify the encoding of the new file. Write the file code such as: F.write (TXT), then txt is a string, it is decoded through the decode string. The encoding of the target file is the culprit that causes the title to refer to the problem. If we open a file: F = open ("Out.html", "W"), under Windows, the default encoding for the new file is GBK, so the Python interpreter uses GBK encoding to parse our network data stream txt, However, the TXT is already decode Unicode encoding, so that will lead to parsing, the above problems occur. The workaround is to change the encoding of the target file: F = open ("Out.html", "w", encoding= ' Utf-8 '). In this way, the problem will cease to exist.

[Python] Unicodeencodeerror: ' GBK ' codec can ' t encode character solution

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.