UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 46:illegal multibyte sequence

First,  when a file is written in python recently, there has been an error as follows:

But the content is unicode encoded content, and do not know how to link the relationship gbk, use encode () and decode () on the content, use gbk, utf-8, gb2312 various codecs to no avail;

Finding information on the Internet, I saw an article about this statement: http://blog.csdn.net/xiaoyi_zhang/article/details/51675099

The conclusion is:

1.'gbk 'codec can not encode character: Description is the Unicode character encoding for the GBK when there is a problem in itself may be the type of Unicode characters, contains some number of characters that can not be converted to GBK encoding;

2. gbk not convert '\ xa0' character, so before you need to convert "\ xa0" replaced; use the String.Replace (U '\ xa0', U '');

I use the code above:

self.file.write(content.replace(u'\xa0', u''))

 The results effectively, without being given;

Another method, the decoded data can not be ignored upon decoding gbk:

 

self.file.write(content.encode("gbk", 'ignore').decode("gbk", "ignore"))

But always felt that this approach is begging the question, then sudden inspiration, this is the time in documents written report errors and evil windows open files by default is "gbk" encoding, may cause does not recognize the unicode characters, then we made the following changes:

self.file = open('biaobai.json', 'w', encoding="utf-8")
self.file.write(content)

The results of OK!

 

 

Transfer: https://www.cnblogs.com/cwp-bg/p/7835434.html

posted @ 2017-11-14 23:40

 

First,  when a file is written in python recently, there has been an error as follows:

But the content is unicode encoded content, and do not know how to link the relationship gbk, use encode () and decode () on the content, use gbk, utf-8, gb2312 various codecs to no avail;

Finding information on the Internet, I saw an article about this statement: http://blog.csdn.net/xiaoyi_zhang/article/details/51675099

The conclusion is:

1.'gbk 'codec can not encode character: Description is the Unicode character encoding for the GBK when there is a problem in itself may be the type of Unicode characters, contains some number of characters that can not be converted to GBK encoding;

2. gbk not convert '\ xa0' character, so before you need to convert "\ xa0" replaced; use the String.Replace (U '\ xa0', U '');

I use the code above:

self.file.write(content.replace(u'\xa0', u''))

 The results effectively, without being given;

Another method, the decoded data can not be ignored upon decoding gbk:

 

self.file.write(content.encode("gbk", 'ignore').decode("gbk", "ignore"))

But always felt that this approach is begging the question, then sudden inspiration, this is the time in documents written report errors and evil windows open files by default is "gbk" encoding, may cause does not recognize the unicode characters, then we made the following changes:

self.file = open('biaobai.json', 'w', encoding="utf-8")
self.file.write(content)

The results of OK!

 

 

Transfer: https://www.cnblogs.com/cwp-bg/p/7835434.html

posted @ 2017-11-14 23:40

 

Guess you like

Origin www.cnblogs.com/yueyuecoding/p/12390983.html