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