Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attribute': 0.05; 'encode': 0.09; 'subject:characters': 0.09; 'encoding': 0.15; 'equivalents': 0.16; 'former,': 0.16; 'inserting': 0.16; 'latter,': 0.16; 'ordinal': 0.16; 'printing.': 0.16; 'wrote:': 0.17; 'bytes': 0.17; 'string,': 0.17; 'unicode': 0.17; '>>>': 0.18; 'skip:p 30': 0.20; 'are:': 0.20; 'written': 0.20; 'import': 0.21; 'work,': 0.22; 'header:In-Reply-To:1': 0.25; 'skip:" 20': 0.26; '(most': 0.27; 'message-id:@mail.gmail.com': 0.27; 'character': 0.29; 'probably': 0.29; 'fri,': 0.30; 'received:209.85.215.46': 0.30; 'code': 0.31; 'point': 0.31; 'file': 0.32; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'nov': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'characters': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'object': 0.38; 'some': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'to:name:python': 0.84; 'do:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=pSQJBVMT2UiHyGUSGfWvYCncLzS3Lselz16ZwPS+RVI=; b=GxdR8asGRqRMmHDZK4iKesnM+rbXKlG949KXGw1IinkSAtDuXSMfksEbA20g04NXU4 0kx9UJNfLWmhM6krl2bNvGd8TdSxUycOsaavXqHWz1cC0mQ0LbavEJkXmzSjWh5PVh/G 83NVF7iecfw0JpaoIE94mQFDn2zPDLQ0bV82tiGdNi5usXNmYW5CTHcYqPe5TLut9rRb G2T9bEFzXz1g+nMkPSulJTAYWL+poCFDaAxk3miyT44gAF0i3G17RsQKnuYK0VSFm8h6 uA5ZRutpN179yGedkdu3PqH+s+AmmjFMYYJlKlbW5n6OYaW4DVmwecSuONEd549wWMlv CpLw== MIME-Version: 1.0 In-Reply-To: References: <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> From: Ian Kelly Date: Fri, 9 Nov 2012 15:10:39 -0700 Subject: Re: Printing characters outside of the ASCII range To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352499071 news.xs4all.nl 6891 [2001:888:2000:d::a6]:52999 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33059 On Fri, Nov 9, 2012 at 2:46 PM, danielk wrote: > D:\home\python>pytest.py > Traceback (most recent call last): > File "D:\home\python\pytest.py", line 1, in > print(chr(253).decode('latin1')) > AttributeError: 'str' object has no attribute 'decode' > > Do I need to import something? Ramit should have written "encode", not "decode". But the above still would not work, because chr(253) gives you the character at *Unicode* code point 253, not the character with CP437 ordinal 253 that your terminal can actually print. The Unicode equivalents of those characters are: >>> list(map(ord, bytes([252, 253, 254]).decode('cp437'))) [8319, 178, 9632] So these are what you would need to encode to CP437 for printing. >>> print(chr(8319)) =E2=81=BF >>> print(chr(178)) =C2=B2 >>> print(chr(9632)) =E2=96=A0 That's probably not the way you want to go about printing them, though, unless you mean to be inserting them manually. Is the data you get from your database a string, or a bytes object? If the former, just do: print(data.encode('cp437')) If the latter, then it should be printable as is, unless it is in some other encoding than CP437.