Python error: Attributeerror: ' Module ' object has no attribute ' setdefaultencoding ' problem resolution

Source: Internet
Author: User
Python's character set processing is really egg ache, currently use UTF-8 majority, then the default character set is ASCII, so we need to change to Utf-8
View current system Character set
Copy CodeThe code is as follows:


Import Sys
Print sys.getdefaultencoding ()


Perform:
Copy CodeThe code is as follows:


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


Modify into Utf-8
Copy CodeThe code is as follows:


Import Sys

Sys.setdefaultencoding (' Utf-8 ')

Print sys.getdefaultencoding ()


Perform:
Copy CodeThe code is as follows:


[Root@lee ~]# python a.py
Traceback (most recent):
File "a.py", line 4, in
Sys.setdefaultencoding (' Utf-8 ')
Attributeerror: ' Module ' object has no attribute ' setdefaultencoding '
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 reload first
Copy the Code code as follows:


Import Sys

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

Print sys.getdefaultencoding ()


Perform
Copy the Code code 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.