urllib.quote() throws KeyError

前端 未结 3 1233
情话喂你
情话喂你 2020-12-25 10:19

To encode the URI, I used urllib.quote(\"schönefeld\") but when some non-ascii characters exists in string, it thorws

KeyError: u\'\\xe9\'
Code:         


        
相关标签:
3条回答
  • 2020-12-25 11:08

    I had the exact same error as @underscore but in my case the problem was that map(quoter,s) tried to look for the key u'\xe9' which was not in the _safe_map. However \xe9 was, so I solved the issue by replacing u'\xe9' by \xe9 in s.

    Moreover, shouldn't the return statement be within the try/except ? I also had to change this to completely solve the problem.

    0 讨论(0)
  • 2020-12-25 11:12

    You are trying to quote Unicode data, so you need to decide how to turn that into URL-safe bytes.

    Encode the string to bytes first. UTF-8 is often used:

    >>> import urllib
    >>> urllib.quote(u'sch\xe9nefeld')
    /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py:1268: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
      return ''.join(map(quoter, s))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 1268, in quote
        return ''.join(map(quoter, s))
    KeyError: u'\xe9'
    >>> urllib.quote(u'sch\xe9nefeld'.encode('utf8'))
    'sch%C3%A9nefeld'
    

    However, the encoding depends on what the server will accept. It's best to stick to the encoding the original form was sent with.

    0 讨论(0)
  • 2020-12-25 11:15

    By just converting the string to unicode I resolved the issue.

    here is the snippet:

    try:
        unicode(mystring, "ascii")
    except UnicodeError:
        mystring = unicode(mystring, "utf-8")
    else:
        pass
    

    Detailed description of solution can be found at http://effbot.org/pyfaq/what-does-unicodeerror-ascii-decoding-encoding-error-ordinal-not-in-range-128-mean.htm

    0 讨论(0)
提交回复
热议问题