Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with urlencode in python

I have this:

a = {'album': u'Metamorphine', 'group': 'monoku', 'name': u'Son Of Venus (Danny\xb4s Song)', 'artist': u'Leandra', 'checksum': '2836e33d42baf947e8c8adef48921f2f76fcb37eea9c50b0b59d7651', 'track_number': 8, 'year': '2008', 'genre': 'Darkwave', 'path': u'/media/data/musik/Leandra/2008. Metamorphine/08. Son Of Venus (Danny\xb4s Song).mp3', 'user_email': '[email protected]', 'size': 6624104} data = urllib.urlencode(mp3_data) 

And that raise an exception:

Traceback (most recent call last):   File "playkud.py", line 44, in <module>     main()   File "playkud.py", line 29, in main     craw(args, options.user_email, options.group)   File "/home/diegueus9/workspace/playku/src/client/playkud/crawler/crawler.py", line 76, in craw     index(root, file, data, user_email, group)   File "/home/diegueus9/workspace/playku/src/client/playkud/crawler/crawler.py", line 58, in index     done = add_song(data[mp3file])   File "/home/diegueus9/workspace/playku/src/client/playkud/service.py", line 32, in add_song     return make_request(URL+'add_song/', data)   File "/home/diegueus9/workspace/playku/src/client/playkud/service.py", line 14, in make_request     data = urllib.urlencode(dict([k.encode('utf-8'),v] for k,v in mp3_data.items()))   File "/usr/lib/python2.5/urllib.py", line 1250, in urlencode     v = quote_plus(str(v)) UnicodeEncodeError: 'ascii' codec can't encode character u'\xb4' in position 19: ordinal not in range(128) 

and with ipython (2.5):

In [7]: urllib.urlencode(a) UnicodeEncodeError                        Traceback (most recent call last)  /home/diegueus9/<ipython console> in <module>()  /usr/lib/python2.5/urllib.pyc in urlencode(query, doseq)    1248         for k, v in query:    1249             k = quote_plus(str(k)) -> 1250             v = quote_plus(str(v))    1251             l.append(k + '=' + v)    1252     else:  UnicodeEncodeError: 'ascii' codec can't encode character u'\xb4' in position 19: ordinal not in range(128) 

How i can fix it?

like image 858
diegueus9 Avatar asked Jun 25 '10 20:06

diegueus9


People also ask

What is Urlencode in Python?

urlencode() function. This is a convenience function which takes a dictionary of key value pairs or a sequence of two-element tuples and uses the quote_plus() function to encode every value. The resulting string is a series of key=value pairs separated by & character. Let's see an example - >>> import urllib.

What does Urllib parse Urlencode do?

parse. urlencode() method can be used for generating the query string of a URL or data for a POST request.

What is the use of Urlencode?

The UrlEncode(String) method can be used to encode the entire URL, including query-string values. If characters such as blanks and punctuation are passed in an HTTP stream without encoding, they might be misinterpreted at the receiving end.

How do you pass special characters in a URL in Python?

s = urllib2. quote(s) # URL encode. # Now "s" is encoded the way you need it. It works!


1 Answers

The urlencode library expects data in str format, and doesn't deal well with Unicode data since it doesn't provide a way to specify an encoding. Try this instead:

mp3_data = {'album': u'Metamorphine',      'group': 'monoku',      'name': u'Son Of Venus (Danny\xb4s Song)',      'artist': u'Leandra',      'checksum': '2836e33d42baf947e8c8adef48921f2f76fcb37eea9c50b0b59d7651',      'track_number': 8,      'year': '2008', 'genre': 'Darkwave',      'path': u'/media/data/musik/Leandra/2008. Metamorphine/08. Son Of Venus (Danny\xb4s Song).mp3',      'user_email': '[email protected]',      'size': 6624104}  str_mp3_data = {} for k, v in mp3_data.iteritems():     str_mp3_data[k] = unicode(v).encode('utf-8') data = urllib.urlencode(str_mp3_data) 

What I did was ensure that all data is encoded into str using UTF-8 before passing the dictionary into the urlencode function.

like image 111
Walter Mundt Avatar answered Sep 21 '22 08:09

Walter Mundt