"More About Unicode in Python 2 and 3"

Roy Smith roy at panix.com
Sun Jan 5 23:49:47 EST 2014


In article <mailman.5004.1388983234.18130.python-list at python.org>,
 Tim Chase <python.list at tim.thechases.com> wrote:

> On 2014-01-05 23:24, Roy Smith wrote:
> > $ hexdump data
> > 0000000 d7 a8 a3 88 96 95
> > 
> > That's EBCDIC for "Python".  What would I write in Python 3 to read
> > that file and print it back out as utf-8 encoded Unicode?
> > 
> > Or, how about a slightly different example:
> > 
> > $ hexdump data
> > 0000000 43 6c 67 75 62 61
> > 
> > That's "Python" in rot-13 encoded ascii.  How would I turn that
> > into cleartext Unicode in Python 3?
> 
> 
> tim at laptop$ python3
> Python 3.2.3 (default, Feb 20 2013, 14:44:27) 
> [GCC 4.7.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> s1 = b'\xd7\xa8\xa3\x88\x96\x95'
> >>> s1.decode('ebcdic-cp-be')
> 'Python'
> >>> s2 = b'\x43\x6c\x67\x75\x62\x61'
> >>> from codecs import getencoder
> >>> getencoder("rot-13")(s2.decode('utf-8'))[0]
> 'Python'
> 
> -tkc

Thanks.  But, I see I didn't formulate my problem statement well.  I was 
(naively) assuming there wouldn't be a built-in codec for rot-13.  Let's 
assume there isn't; I was trying to find a case where you had to treat 
the data as integers in one place and text in another.  How would you do 
that?



More information about the Python-list mailing list