Python quote error: * * * keyerror: u ‘\ uxx’ [How to Solve]

Problem: urllib.quote (xxx) error after running * * * keyerror: u '\ uxx'
reason: look at XXX type: & lt; type 'unicode'>, and params in urlib.quote (params) is a string. You can only guess that the encoding of the string does not meet the requirements, so the code XXX
solution: urlib.quote (XXX. Encode ("UTF-8") or urlib.quote (STR (xxx))

Examples are as follows:

>>> import urllib
>>> xxx = u'[{"test":"Test"}]'
>>> urllib.quote(xxx)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1298, in quote
    return ''.join(map(quoter, s))
KeyError: u'\u6d4b'
>>> type(xxx)
<type 'unicode'>
>>> urllib.quote(xxx.encode("utf-8"))
'%5B%7B%22test%22%3A%22%E6%B5%8B%E8%AF%95%22%7D%5D'
>>> urllib.quote(str(xxx))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-11: ordinal not in range(128)
>>> import sys
>>> sys.setdefaultencoding('utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'setdefaultencoding'
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('utf-8')
>>> urllib.quote(str(xxx))
'%5B%7B%22test%22%3A%22%E6%B5%8B%E8%AF%95%22%7D%5D'

Read More: