python3 raw strings and \u escapes
MRAB
python at mrabarnett.plus.com
Fri Jun 15 20:20:28 EDT 2012
On 16/06/2012 00:42, Jason Friedman wrote:
> This is a related question.
>
> I perform an octal dump on a file:
> $ od -cx file
> 0000000 h e l l o w o r l d \n
> 6568 6c6c 206f 6f77 6c72 0a64
>
> I want to output the names of those characters:
> $ python3
> Python 3.2.3 (default, May 19 2012, 17:01:30)
> [GCC 4.6.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import unicodedata
>>>> unicodedata.name("\u0068")
> 'LATIN SMALL LETTER H'
>>>> unicodedata.name("\u0065")
> 'LATIN SMALL LETTER E'
>
> But, how to do this programatically:
>>>> first_two_letters = "6568 6c6c 206f 6f77 6c72 0a64".split()[0]
>>>> first_two_letters
> '6568'
>>>> first_letter = "00" + first_two_letters[2:]
>>>> first_letter
> '0068'
>
> Now what?
>>> hex_code = "65"
>>> unicodedata.name(chr(int(hex_code, 16)))
'LATIN SMALL LETTER E'
More information about the Python-list
mailing list