Unicode and UrlEncode!

John johng2001 at rediffmail.com
Thu Mar 18 00:12:55 CET 2004

I am trying to translate some French via Google.

Here is my code.

# -*- coding: latin-1 -*-

import httplib, urllib, re

def translate_french_to_english(french):
    params = urllib.urlencode( {'text': french, 'langpair': 'fr|en',
'hl':'en', 'ie':'UTF8', 'oe':'UTF8'} )
    print params
    headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
    Cn = httplib.HTTPConnection("translate.google.com")
    Cn.request("POST", "/translate_t", params, headers)
    response = Cn.getresponse()
    data = response.read()
    match = re.compile('<textarea name=q
    translated = match.groups()[0]
    return translated

print translate_french_to_english('sÈdimentation')

The problem is my params in encoding to
-- s%C8dimentation --
when it should encode to
-- s%C3%88dimentation --

What should I do?

More information about the Python-list mailing list