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.

RadiantHex
  • 24,907
  • 47
  • 148
  • 244

1 Answers1

2

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
  • 130,178
  • 23
  • 148
  • 122
  • Thank you very much for your reply. How would I go on about unicode characters? Or characters extracted from an html page? – RadiantHex Jan 20 '10 at 17:17
  • 1
    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 Jan 20 '10 at 17:19
  • Thank you Thomas. Indeed very helpful! :) I'll look into this. – RadiantHex Jan 20 '10 at 17:21