Unicode URL encode / decode with Python

2019-05-21 01:36发布

I have an Android app which uses

URLEncoder.encode(S.getSongArtist(),"UTF-8")

to encode a unicode string that is posted to a AppEngine python (2.7) web service. On the service I use

urllib.unquote_plus(artist)

This is not giving me correct results. I have an input like this:

Marie+Lafor%C3%AAt

which is unquote'd to

Marie Laforêt

If I use a javascript url decode, for instance: http://meyerweb.com/eric/tools/dencoder/ I get

Marie Laforêt

A correct result.

I tried using

urllib.unquote(artist).decode('utf-8') 

but this generates an exception. Any hints at all are greatly appreciated.

EDIT

Taxellool had the right answer in the comments:

what you are trying to decode is already decoded. try this:

urllib.unquote_plus(artist.encode('utf-8')).decode('utf-8')

2条回答
做自己的国王
2楼-- · 2019-05-21 01:49

Taxellool had the right answer in the comments:

what you are trying to decode is already decoded. try this:

urllib.unquote_plus(artist.encode('utf-8')).decode('utf-8')
查看更多
Ridiculous、
3楼-- · 2019-05-21 01:52

I guess you are decoding before urllib.unquote():

>>> print urllib.unquote_plus('Marie+Lafor%C3%AAt'.decode('utf-8'))  
Marie Laforêt

If you decode after unquote, result would be what you want:

>>> print urllib.unquote_plus('Marie+Lafor%C3%AAt').decode('utf-8')  
Marie Laforêt

Just make sure you don't pass a unicode to urllib.unquote_plus.

查看更多
登录 后发表回答