Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:Python': 0.06; 'encoded': 0.07; 'python3': 0.07; 'utf-8': 0.07; 'python': 0.11; '"python"': 0.16; '-tkc': 0.16; 'codecs': 0.16; 'ebcdic': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'roy': 0.16; 'subject:More': 0.16; 'subject:Unicode': 0.16; 'unicode?': 0.16; 'wrote:': 0.18; 'skip:g 40': 0.19; 'slightly': 0.19; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'unicode': 0.24; 'header:In-Reply-To:1': 0.27; 'subject:About': 0.31; 'file': 0.32; 'charset:us-ascii': 0.36; 'turn': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'subject:"': 0.60; 'back': 0.62; 'more': 0.64; 'different': 0.65; 'smith': 0.68; 'received:50.22': 0.84; '2013,': 0.91 Date: Sun, 5 Jan 2014 22:41:23 -0600 From: Tim Chase To: python-list@python.org Subject: Re: "More About Unicode in Python 2 and 3" In-Reply-To: References: <52CA13BD.4050708@stoneleaf.us> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1388983234 news.xs4all.nl 2876 [2001:888:2000:d::a6]:36274 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63267 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@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