Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if a python string is printable

Tags:

python

string

I have some code that pulls data from a com-port and I want to make sure that what I got really is a printable string (i.e. ASCII, maybe UTF-8) before printing it. Is there a function for doing this? The first half dozen places I looked, didn't have anything that looks like what I want. (string has printable but I didn't see anything (there, or in the string methods) to check if every char in one string is in another.

Note: control characters are not printable for my purposes.


Edit: I was/am looking for a single function, not a roll-your-own solution:

What I ended up with is:

all(ord(c) < 127 and c in string.printable for c in input_str) 
like image 265
BCS Avatar asked Sep 03 '10 14:09

BCS


People also ask

Is printable () in Python?

The isprintable() method returns True if all the characters are printable, otherwise False. Example of none printable character can be carriage return and line feed.

How do you check whether all the characters in string is printable?

The isprintable() method returns “True” if all characters in the string are printable or the string is empty, Otherwise, It returns “False”. This function is used to check if the argument contains any printable characters such as: Digits ( 0123456789 ) Uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )

What is not printable in Python?

Non-printable characters are characters that are not visible and do not occupy a space in printing. Some characters in the Unicode character database as "Other" or "Separator" are non-printable.

How do I know if a character is printable?

C isprint()The isprint() function checks whether a character is a printable character or not. Those characters that occupies printing space are known as printable characters. Printable characters are just the opposite of control characters which can be checked using iscntrl().


2 Answers

As you've said the string module has printable so it's just a case of checking if all the characters in your string are in printable:

>>> hello = 'Hello World!' >>> bell = chr(7) >>> import string >>> all(c in string.printable for c in hello) True >>> all(c in string.printable for c in bell) False 

You could convert both strings to sets - so the set would contain each character in the string once - and check if the set created by your string is a subset of the printable characters:

>>> printset = set(string.printable) >>> helloset = set(hello) >>> bellset = set(bell) >>> helloset set(['!', ' ', 'e', 'd', 'H', 'l', 'o', 'r', 'W']) >>> helloset.issubset(printset) True >>> set(bell).issubset(printset) False 

So, in summary, you would probably want to do this:

import string printset = set(string.printable) isprintable = set(yourstring).issubset(printset) 
like image 114
Dave Webb Avatar answered Oct 11 '22 07:10

Dave Webb


try/except seems the best way:

def isprintable(s, codec='utf8'):     try: s.decode(codec)     except UnicodeDecodeError: return False     else: return True 

I would not rely on string.printable, which might deem "non-printable" control characters that can commonly be "printed" for terminal control purposes (e.g., in "colorization" ANSI escape sequences, if your terminal is ANSI-compliant). But that, of course, depends on your exact purposes for wanting to check this!-)

like image 41
Alex Martelli Avatar answered Oct 11 '22 07:10

Alex Martelli