Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33059

Re: Printing characters outside of the ASCII range

References <3d4644f8-ab88-41c5-9a52-2a5678dd64c0@googlegroups.com> <mailman.3508.1352483285.27098.python-list@python.org> <99d5bd83-35ab-4801-b953-391c497c35bf@googlegroups.com> <mailman.3517.1352496859.27098.python-list@python.org> <ba72452f-fac6-4b18-9b22-d1854eecbffb@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-11-09 15:10 -0700
Subject Re: Printing characters outside of the ASCII range
Newsgroups comp.lang.python
Message-ID <mailman.3521.1352499071.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Nov 9, 2012 at 2:46 PM, danielk <danielkleinad@gmail.com> wrote:
> D:\home\python>pytest.py
> Traceback (most recent call last):
>   File "D:\home\python\pytest.py", line 1, in <module>
>     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))
ⁿ
>>> print(chr(178))
²
>>> print(chr(9632))
■

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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 09:17 -0800
  Re: Printing characters outside of the ASCII range Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-09 10:34 -0700
  Re: Printing characters outside of the ASCII range Andrew Berg <bahamutzero8825@gmail.com> - 2012-11-09 11:39 -0600
  Re: Printing characters outside of the ASCII range Dave Angel <d@davea.name> - 2012-11-09 12:47 -0500
    Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 13:17 -0800
      RE: Printing characters outside of the ASCII range "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-09 21:34 +0000
        Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 13:46 -0800
          Re: Printing characters outside of the ASCII range Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-09 15:10 -0700
            Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-11 05:42 -0800
              Re: Printing characters outside of the ASCII range Lele Gaifax <lele@metapensiero.it> - 2012-11-11 18:09 +0100
            Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-11 05:42 -0800
        Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 13:46 -0800
      Re: Printing characters outside of the ASCII range Andrew Berg <bahamutzero8825@gmail.com> - 2012-11-09 15:39 -0600
    Re: Printing characters outside of the ASCII range danielk <danielkleinad@gmail.com> - 2012-11-09 13:17 -0800
  Re: Printing characters outside of the ASCII range wxjmfauth@gmail.com - 2012-11-10 02:09 -0800
  Re: Printing characters outside of the ASCII range Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-11-11 15:40 +0100

csiph-web