Python error: AttributeError: 'module' object has no attribute 'setdefaultencoding 'solution: setdefaultencoding
Python character set processing is really cool, the current use of UTF-8, and then the default use of the character set is ascii, so we need to change to UTF-8
View the current system Character Set
Copy codeThe Code is as follows:
Import sys
Print sys. getdefaultencoding ()
Run:
Copy codeThe Code is as follows:
[Root @ lee ~] # Python a. py
Ascii
Change to UTF-8
Copy codeThe Code is as follows:
Import sys
Sys. setdefaultencoding ('utf-8 ')
Print sys. getdefaultencoding ()
Run:
Copy codeThe Code is as follows:
[Root @ lee ~] # Python a. py
Traceback (most recent call last ):
File "a. py", line 4, in <module>
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.
Copy codeThe Code is as follows:
Import sys
Reload (sys)
Sys. setdefaultencoding ('utf-8 ')
Print sys. getdefaultencoding ()
Run
Copy codeThe Code is as follows:
[Root @ lee ~] # Python a. py
UTF-8
AttributeError: 'module' object has no attribute 'urlencode' is displayed in the Python getting started error'
It's python 3. Use urllib. parse.
Import urllib. parseimport urllib. requesturl = 'www .baidu.com/s'user_agent = 'mozilla/4.0 (compatible; MSIE 5.5; Windows NT) 'values = {'name': 'why', 'location': 'sdu ', 'language': 'python', 'ie': 'utf-8', 'wd ': 'python'} headers = {'user-agent ': user_agent} data = urllib. parse. urlencode (values) # data = data. encode (encoding = 'utf8') req = urllib. request. request (url + '? '+ Data) #, data, headers) response = urllib. request. urlopen (req) the_page = response. read () print (the_page.decode ('utf8 '))
Python networkx program running error AttributeError: 'module' object has no attribute
From icmodel import independent_cascade has been imported
Nx. independent_cascade. Delete
H = independent_cascade (DG, [6])