Python Solve unicodeencodeerror ' gbk '/' ASCII '/' UTF8 ' codec can ' t encode character ' \x?? ' in position? Troubleshoot Python-encoded errors

Source: Internet
Author: User

In Python, the processing of Chinese characters has always been a headache, a word is garbled, and the cause of garbled is not the same, sometimes Python itself default encoder settings are not correct, and sometimes the use of the IDE decoder is not correct, There are sometimes terminal terminal decoder is not correct, the same code in the Python2 on the normal operation, Python3 on it, anyway, there are many reasons for garbled, here are some of the bloggers have encountered some errors and solutions:

Error 1:

Unicodeencodeerror: ' GBK ' codec can ' t encode character ' \x?? ' in position?: illegal multibyte sequence

This is in the Win7 with Python3 compile time encountered, because Win7 python3 default encoding is not UTF8, we just need to change the default encoding to UTF8 to solve the problem, see the following code:

Import= io. Textiowrapper (sys.stdout.buffer,encoding='UTF8'# Change default encoding To UTF8  

Error 2:

Unicodedecodeerror: ' ASCII ' codec can ' t decode byte 0x?? In Position:ordinal not in range (128)

This is the Mac on the use of Python2 compile time encountered, because it is also because the default encoding python2 is not UTF8, we only need to change the default encoding UTF8 to solve the problem, see the following code:

Import sysreload (SYS) sys.setdefaultencoding ('utf-8')

Like other causes of errors, such as the IDE display garbled, or terminal terminal display garbled, you can quickly find a solution by Google search, here will not repeat.

Resources:

Http://ajucs.com/2015/11/10/Python-character-encoding-explained.html

Python Solve unicodeencodeerror ' gbk '/' ASCII '/' UTF8 ' codec can ' t encode character ' \x?? ' in position? Troubleshoot Python-encoded errors

Related Article

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.