urllib.quote() throws KeyError

前端 未结 3 1250
情话喂你
情话喂你 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: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

提交回复
热议问题