Unicodeencodeerror: ' GBK ' codec can ' t encode character ' \xa0 ' in position 46:illegal multibyte sequence

Source: Internet
Author: User

The following error occurred when writing to a file using Python recently:

But content is Unicode encoding, do not know how and GBK pull on the relationship, the content using encode () and Decode (), with gbk,utf-8,gb2312 all kinds of coding decoding have no effect;

Find information on the Internet, see an article about this saying: http://blog.csdn.net/xiaoyi_zhang/article/details/51675099

The conclusion is:

1. ' GBK ' codec can ' t encode character: Description is a problem when encoding Unicode characters to GBK, possibly a character of its own Unicode type, containing some characters that cannot be converted to GBK encoding;

2. GBK cannot convert ' \xa0 ' characters, so "\xa0" needs to be replaced before conversion, using String.Replace (U' \xa0 ', U');

I use it in the above code:

Self.file.write (Content.replace (U'\xa0', u'))

The result is effective, no error;

But always think that this method is to avoid problems, and then burst inspiration, this is in the file write the error, and the evil windows open file by default is "GBK" encoded, may cause unrecognized Unicode characters, then made the following changes:

Self.file = open (' Biaobai.json ', ' W ', encoding= "Utf-8")
Self.file.write (content)

Result ok!

Unicodeencodeerror: ' GBK ' codec can ' t encode character ' \xa0 ' in position 46:illegal multibyte sequence

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.