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


Groups > comp.lang.python > #104633

Re: non printable (moving away from Perl)

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: non printable (moving away from Perl)
Date 2016-03-12 06:52 +1100
Message-ID <mailman.2.1457725975.12893.python-list@python.org> (permalink)
References <nbt27u$fe7$1@gioia.aioe.org> <mailman.0.1457724244.12893.python-list@python.org> <nbv6n7$1eu7$1@gioia.aioe.org>

Show all headers | View raw


Fillmore <fillmore_remove@hotmail.com> writes:

> On 3/11/2016 2:23 PM, MRAB wrote:
> > Python 3 (Unicode) strings have an .isprintable method:
> >
> > mystring.isprintable()
>
> my strings are UTF-8. Will it work there too?

You need to always be clear on the difference between text (the Python 3
‘str’ type) versus bytes.

It only makes sense to talk about an encoding, when talking about bytes.

Text itself is an abstract data type; the content of a Unicode string
does not have any encoding because it is not encoded.

The content of a byte stream (such as a file's content) is not text, it
is bytes.

    >>> foo = "こんにちは"
    >>> foo.isprintable()
    True

    >>> foo_encoded = foo.encode("utf-8")
    >>> foo_encoded.isprintable()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'bytes' object has no attribute 'isprintable'

You can only ask ‘isprintable’ about text. Bytes are not printable
because bytes are not text; you need to decode the bytes to text before
asking whether that text is printable.

    >>> infile = open('lorem.txt', 'rb')
    >>> infile_bytes = infile.read()
    >>> infile_bytes.isprintable()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'bytes' object has no attribute 'isprintable'

    >>> infile = open('lorem.txt', 'rt', encoding="utf-8")
    >>> infile_text = infile.read()
    >>> infile_text.isprintable()
    True

-- 
 \        “Telling pious lies to trusting children is a form of abuse, |
  `\                    plain and simple.” —Daniel Dennett, 2010-01-12 |
_o__)                                                                  |
Ben Finney

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


Thread

non printable (moving away from Perl) Fillmore <fillmore_remove@hotmail.com> - 2016-03-10 19:07 -0500
  Re: non printable (moving away from Perl) Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-10 17:25 -0700
  Re: non printable (moving away from Perl) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-11 01:30 +0000
  Re: non printable (moving away from Perl) Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-10 20:52 -0700
  Re: non printable (moving away from Perl) Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-03-11 13:13 +0100
    Re: non printable (moving away from Perl) Fillmore <fillmore_remove@hotmail.com> - 2016-03-11 09:23 -0500
      Re: non printable (moving away from Perl) Peter Otten <__peter__@web.de> - 2016-03-11 16:22 +0100
      Re: non printable (moving away from Perl) Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-03-11 17:34 +0100
      Re: non printable (moving away from Perl) Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-11 10:08 -0700
  Re: non printable (moving away from Perl) Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-03-11 13:17 +0100
    Re: non printable (moving away from Perl) Marko Rauhamaa <marko@pacujo.net> - 2016-03-11 14:47 +0200
  Re: non printable (moving away from Perl) MRAB <python@mrabarnett.plus.com> - 2016-03-11 19:23 +0000
    Re: non printable (moving away from Perl) Fillmore <fillmore_remove@hotmail.com> - 2016-03-11 14:36 -0500
      Re: non printable (moving away from Perl) Ben Finney <ben+python@benfinney.id.au> - 2016-03-12 06:52 +1100

csiph-web