Fillmore <fillmore_rem...@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 -- https://mail.python.org/mailman/listinfo/python-list