Python print output text display UnicodeEncodeError: 'gbk' codec can not encode character '\ xa0' in position 30: illegal multibyte sequence

There is a practice test, you need to climb something, the result has been an error UnicodeEncodeError: ‘gbk’ codec can’t encode character ‘\xa0’ in positionin a search online and found a problem in Windows console. Console coding is GBK, Python is UTF-8, creating a conflict. Here are three solutions:

The first method: direct replacement error content

 
 
print(json.dumps(textList, ensure_ascii=False).replace('\xa0', ' '))

 

The second method: re-decoded

First with GBK encoding, add ignore discarding erroneous character, and then decoded.

 

import requests
url = 'https://segmentfault.com/a/1190000015617318'
print(requests.get(url).text.encode('gbk', 'ignore').decode('gbk')

 

 

 

The third method: encoding modified console

Windows Registry Editor Version 5.00
 
[HKEY_CURRENT_USER\Console\%SystemRoot%_system32_cmd.exe]
"CodePage"=dword:0000fde9
"FontFamily"=dword:00000036
"FontWeight"=dword:00000190
"FaceName"="Consolas"
"ScreenBufferSize"=dword:232900d2
"WindowSize"=dword:002b00d2

 

After the save operation. If Ctrl + B invalid, try again to open the program with .py python.exe.

Reprint:

https://blog.csdn.net/weixin_34292959/article/details/88765900

Guess you like

Origin www.cnblogs.com/liruilong/p/12498447.html