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

前端 未结 8 757
予麋鹿
予麋鹿 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:32

    Nothing new to add except to point out that the urlencode algorithm is nothing tricky. Rather than processing your data once and then calling urlencode on it, it would be perfectly fine to do something like:

    from urllib import quote_plus
    
    def urlencode_utf8(params):
        if hasattr(params, 'items'):
            params = params.items()
        return '&'.join(
            (quote_plus(k.encode('utf8'), safe='/') + '=' + quote_plus(v.encode('utf8'), safe='/')
                for k, v in params))
    

    Looking at the source code for the urllib module (Python 2.6), their implementation does not do much more. There is an optional feature where values in the parameters that are themselves 2-tuples are turned into separate key-value pairs, which is sometimes useful, but if you know you won't need that, the above will do.

    You can even get rid of the if hasattr('items', params): if you know you won't need to handle lists of 2-tuples as well as dicts.

提交回复
热议问题