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