trihaitran 0 Light Poster

Hi, I'm writing a dictionary program with Turbogears that involves using Chinese text. There is an HTML form that has a dropdown menu, a text field and a button. The input from the form (in this case a Chinese character) is passed to one of my methods. The thing is that my code requires this command:

@expose()
def search(self, method, query):
    if method == 'Chinese':
        query_unicode = query.decode('utf8')

This gives me an error of:

File "/Users/username/Desktop/Dictionary/dictionary/controllers.py", line 42, in search
    query_unicode = query.decode('utf8')
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u6c34' in position 0: ordinal not in range(128)

The odd thing here is that if I do this in the interpreter this is what happens:

>>> character = '\346\260\264'
>>> character_unicode = character.decode('utf8')
>>> character_unicode
u'\u6c34'

Why is Turbogears giving me an error for using the decode() method when the interpreter does not? Has anyone else made an application with Turbogears that deals with non-ASCII international text?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.