Converting numbers to unicode charaters

Peter Otten __peter__ at web.de
Mon Sep 24 18:24:22 CEST 2007


Duncan Booth wrote:

> Laurent Pointal <laurent.pointal at limsi.fr> wrote:
> 
>> You may eventually use a more condensed expression and avoid n 
>> concatenation of n chars using join, like this:
>> 
>> >>> u''.join(unichr(int(x,16)) for x in ['42','72','61','64'])
>> u'Brad'
> 
> Or even avoid the loop completely:
> 
>>>> hexs = ['42', '72', '61', '64']
>>>> u'\\x'.join(['']+hexs).decode('string-escape')
> 'Brad'
> 
> (but for that to work you really do need to be sure that all the values are 
> 2 characters).

Or

>>> "".join(['42', '72', '61', '74']).decode("hex").decode("latin1")
u'Brat'

(same caveat)

Peter



More information about the Python-list mailing list