urllib.urlencode doesn't like unicode values: how about this workaround?

前端 未结 8 737
予麋鹿
予麋鹿 2020-12-12 18:59

If I have an object like:

d = {\'a\':1, \'en\': \'hello\'}

...then I can pass it to urllib.urlencode, no problem:



        
8条回答
  •  独厮守ぢ
    2020-12-12 19:53

    I had the same problem with German "Umlaute". The solution is pretty simple:

    In Python 3+, urlencode allows to specify the encoding:

    from urllib import urlencode
    args = {}
    args = {'a':1, 'en': 'hello', 'pt': u'olá'}
    urlencode(args, 'utf-8')
    
    >>> 'a=1&en=hello&pt=ol%3F'
    

提交回复
热议问题