<p>I use to run</p>

<pre><code>$s =~ s/[^[:print:]]//g;
</code></pre>

<p>on Perl to get rid of non printable characters. </p>

<p>In Python there's no POSIX regex classes, and I can't write [:print:] having it mean what I want. I know of no way in Python to detect if a character is printable or not. </p>

<p>What would you do? </p>

<p>EDIT: It has to support Unicode characters as well. The string.printable way will happily strip them out of the output. 
curses.ascii.isprint will return false for any unicode character.</p>

The best I've come up with now is (thanks to the python-izers above)
```
```
def filter_non_printable(str):
  return ''.join([c for c in str if ord(c) > 31 or ord(c) == 9])
```
```
This is the only way I've found out that works with Unicode characters/strings
Any better options?