views:

413

answers:

1

I use Google App Engine and cannot use any C/C++ extension, just pure & pythonic library to do conversion of Unicode/UTF-8 strings to lower/upper case. str.lower() and string.lowercase() don't.

+5  A: 

str encoded in UTF-8 and unicode are two different types. Don't use string, use the appropriate method on the unicode object:

>>> print u'ĉ'.upper()
Ĉ

Decode str to unicode before using:

>>> print 'ĉ'.decode('utf-8').upper()
Ĉ
Ignacio Vazquez-Abrams
+1 Thanks. How can I convert unicode type to UTF-8?
Viet
`>>> print repr(u'ĉ'.encode('utf-8'))` `'\xc4\x89'`
Ignacio Vazquez-Abrams
Thanks. Is this applicable to Vietnamese?
Viet
It should be. It's not hard to test in the interactive interpreter.
Ignacio Vazquez-Abrams
My code does not work for Russian and Vietnamese. I don't know other languageshttp://oladic.appspot.com/add/ОИЧУНКАЛСhttp://oladic.appspot.com/add/TÌNH%20YÊUhttp://oladic.appspot.com/add/ĉĉĉĉ
Viet
Finally it worked! Thank you very much! I wish I could vote more!
Viet
Viet: you probably want to URL-encode unicode characters if you're putting them in a URL (although it's probably easier to just POST them as utf-8, assuming you're using a form to submit them).
Wooble