A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/327269.html below:

Most direct way to strip unoprintable characters out of a string?

Most direct way to strip unoprintable characters out of a string?George Sakkis gsakkis at rutgers.edu
Sat Sep 24 18:03:52 EDT 2005
"Steve Bergman" <steve at rueb.com> wrote:

> When sanitizing data coming in from HTML forms, I'm doing this (lifted
> from the Python Cookbook):
>
> from string import maketrans, translate, printable
> allchars = maketrans('','')
> delchars = translate(allchars, allchars, printable)
> input_string = translate(input_string, allchars, delchars)
>
> Which is OK.  But it seems like there should be more straightforward way
> that I just haven't figured out.  Is there?

If by straightforward you mean one-liner, there is:
''.join(c for c in input_string if c not in string.printable)

If you care about performance though, string.translate is faster; as always, the best way to decide
on a performance issue is to profile the alternatives on your data and see if it's worth going for
the fastest one at the expense of readability.

George



More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4