Re: Unicode question : turn "José" into u"José"

John Machin sjmachin at lexicon.net
Wed Apr 5 17:35:48 EDT 2006


The most important thing that you are missing is that you need to know
the encoding used for the 8-bit-character string. Let's guess that it's
Latin1.
Then all you have to do is use the unicode() builtin function, or the
string decode method.
# >>> s =  'Jos\xe9'
# >>> s
# 'Jos\xe9'
# >>> u = unicode(s, 'latin1')
# >>> u
# u'Jos\xe9'
# >>> u2 = s.decode('latin1')
# >>> u2
# u'Jos\xe9'

Other important things:
(1) Using eval() is not usually the best way to do things.
(2) If your code is not in entirely in ASCII, put a coding declaration
at the top of the source file.




More information about the Python-list mailing list