If I have an object like:
d = {\'a\':1, \'en\': \'hello\'}
...then I can pass it to urllib.urlencode, no problem:
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.