Unicode question : turn "José" into u"José"
Ian Sparks
Ian.Sparks at etrials.com
Wed Apr 5 16:26:20 EDT 2006
This is probably stupid and/or misguided but supposing I'm passed a byte-string value that I want to be unicode, this is what I do. I'm sure I'm missing something very important.
Short version :
>>> s = "José" #Start with non-unicode string
>>> unicoded = eval("u'%s'" % "José")
Long version :
>>> s = "José" #Start with non-unicode string
>>> s #Lets look at it
'Jos\xe9'
>>> escaped = s.encode('string_escape')
>>> escaped
'Jos\\xe9'
>>> unicoded = eval("u'%s'" % escaped)
>>> unicoded
u'Jos\xe9'
>>> test = u"José" #What they should have passed me
>>> test == unicoded #Am I really getting the same thing?
True #Yay!
More information about the Python-list
mailing list