Python error: AttributeError: 'module' objecthasnoattribute 'setdefaultencoding 'solution

Source: Internet
Author: User
This article mainly introduces the python error: AttributeError: moduleobjecthasnoattributesetdefaultencoding problem solution, need friends can refer to the Python character set processing is really cool, at present the use of UTF-8 mostly, the default character set is ascii, so we need to change it to UTF-8.
View the current system Character set

The code is as follows:


Import sys
Print sys. getdefaultencoding ()


Run:

The code is as follows:


[Root @ lee ~] # Python a. py
Ascii


Change to UTF-8

The code is as follows:


Import sys

Sys. setdefaultencoding ('utf-8 ')

Print sys. getdefaultencoding ()


Run:

The code is as follows:


[Root @ lee ~] # Python a. py
Traceback (most recent call last ):
File "a. py", line 4, in
Sys. setdefaultencoding ('utf-8 ')
AttributeError: 'module' object has no attribute 'setdefaultencoding'
Tip: AttributeError: 'module' object has no attribute 'setdefaultencoding '?


Later, after finding relevant information, we found that earlier versions can directly sys. setdefaultencoding ('utf-8'). The new version needs to be reloaded first.

The code is as follows:


Import sys

Reload (sys)
Sys. setdefaultencoding ('utf-8 ')

Print sys. getdefaultencoding ()


Run

The code is as follows:


[Root @ lee ~] # Python a. py
UTF-8

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.