views:

154

answers:

1

I have strings such as

["Tabula Rasa", "façade", "DJ Tiësto"]

I'm accessing Google Ajax API in Python using the base url:

base = 'http://ajax.googleapis.com/ajax/services/search/web'
   '?v=1.0&q=%s'

I'm having issues using these strings plain and noticed I have to transform certain characters,

eg. "Tabula Rasa" -->  "Tabula%20Rasa"

But I have a huge list of these strings and I do not know of a way I can automatically prepare these string for the URL query.

Any help would be very much appreciated.

+2  A: 

What you're looking for is urllib.quote():

>>> urllib.quote("Tabula Rasa")
'Tabula%20Rasa'

The non-ASCII strings may need to be recoded into the encoding expected by the Google AJAX API, if you don't have them in the same encoding already.

Thomas Wouters
Thank you very much for your reply.How would I go on about unicode characters? Or characters extracted from an html page?
RadiantHex
If you have unicode, you first need to encode it into the encoding expected by the Google AJAX API (presumably UTF-8, so `data.encode('utf-8')`.) If you have characters extracted from a HTML page, I would hope you have them in a unicode string -- but if you have them as a bytestring, decode them using the encoding used by the webpage. Then you encode into the right encoding like any other unicode object.
Thomas Wouters
Thank you Thomas. Indeed very helpful! :) I'll look into this.
RadiantHex