How to parse utf-8 encoded query parameter using Python 2.6

I have a wonderful (Scandinavian?) User on my site complaining that I cannot parse his username in the URLs and therefore I am showing him no results on his page on my site.

I am sure that the browser encodes the requests as http://councilroom.com/player?player=G%C3%B6rling

I want the player string to become Görling, not Görling, which will convert to.

I am using web.py with python 2.6 and trying to parse the url like this

parsed_url = urlparse.urlparse(web.ctx.fullpath)
query_dict = dict(urlparse.parse_qsl(parsed_url.query))
target_player = query_dict['player']

Edit: using unutbu I fixed this by changing it to

query_dict = dict(urlparse.parse_qsl(web.ctx.env['QUERY_STRING']))
target_player = query_dict['player'].decode('utf-8')

I think webpy misinterpreted the full path in web.ctx, but the QUERY_STRING variable is not possible.

+3
2
In [4]: import urlparse

In [6]: parsed_url = urlparse.urlparse('http://councilroom.com/player?player=G%C3%B6rling')

In [7]: parsed_url
Out[7]: ParseResult(scheme='http', netloc='councilroom.com', path='/player', params='', query='player=G%C3%B6rling', fragment='')

In [8]: query_dict = dict(urlparse.parse_qsl(parsed_url.query))

In [9]: query_dict
Out[9]: {'player': 'G\xc3\xb6rling'}

.decode('utf-8'):

In [10]: target_player = query_dict['player'].decode('utf-8')

In [11]: target_player
Out[11]: u'G\xf6rling'

In [12]: print(target_player)
Görling

PS. - str 'G\xc3\xb6rling' Görling Görling:

In [3]: print(u'G\xc3\xb6rling')
Görling
0

, , ... , repr(target_player).

'G\xc3\xb6rling', UTF-8 . unicode, . , ... A- , , , cp1252 ( latin1 aka iso-8859-1).

0

All Articles