【编码】UnicodeEncodeError: ‘gbk‘ codec can‘t encode character ‘\xa0‘ in position XXX

Sometimes running the code in win will encounter the error report in the title:

"UnicodeEncodeError:'gbk' codec can't encode character'\xa0' in position XXX", what is confusing is the encoding setting at the beginning of the program# coding:utf-8 and the encoding conversion during data stream IO operation have been Added to the program. Where does gbk come from? In fact, the default encoding of new files in win is gbk.

When we encounter encoding problems, there are three places to pay attention to: the encoding of the file, the encoding of the IDE, and the encoding during the data stream IO operation.

The latter two are mentioned in many shared posts on the Internet, and the first one is often less. It is precisely this problem in the title that the encoding of the file is caused by gbk.

The solution is very simple. Take python's pycharm as an example. Just make sure that the project code and the default code are unified as UTF-8 in the settings. After saving, copy the error code to the newly created file and run it.

Insert picture description here

Guess you like

Origin blog.csdn.net/zhuan_long/article/details/112097930