Python error: Attributeerror: The solution to the problem of ' setdefaultencoding ' has ' the ' module ' object and no attribute _python

Source: Internet
Author: User

Python's character set processing is really a pain in the egg, the current use of UTF-8 mostly, and then the default character set is ASCII, so we need to change to Utf-8
View the current system character set

Copy Code code as follows:

Import Sys
Print sys.getdefaultencoding ()

Perform:
Copy Code code as follows:

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

Modified into Utf-8
Copy Code code as follows:

Import Sys

Sys.setdefaultencoding (' Utf-8 ')

Print sys.getdefaultencoding ()

Perform:
Copy Code code as follows:

[Root@lee ~]# python a.py
Traceback (most recent call last):
File "a.py", line 4, in <module>
Sys.setdefaultencoding (' Utf-8 ')
Attributeerror: ' setdefaultencoding ' of ' module ' object has no attribute
Hint: attributeerror: ' Module ' object has no attribute ' setdefaultencoding '?

Later, after looking for relevant information, only to find that the earlier version can be directly sys.setdefaultencoding (' Utf-8 '), the new version needs to be reload first
Copy Code code as follows:

Import Sys

Reload (SYS)
Sys.setdefaultencoding (' Utf-8 ')

Print sys.getdefaultencoding ()

Perform
Copy Code code as follows:

[Root@lee ~]# python a.py
Utf-8

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.