remove special characters from line

John Machin sjmachin at lexicon.net
Wed Jul 2 09:05:23 EDT 2003


"Chris Rennert" <Chris.Rennert at mdi-oshkosh.com> wrote in message news:<3f01e0b0$0$43854$39cecf19 at news.twtelecom.net>...
> I apologize for being vague on what a "special" character is in my context.
> For me, it would be anything that isn't A..Z , a..z, or 0..9  .
> My list seems to work, or like it was suggested I could have used a string
> as well.  I thank everyone for the help, and I am really enjoying learning
> Python.
> 

That's good news ... and here's a couple of more lessons:

(1) You can use the translate method:

# once
import string
id_trans = "".join([chr(x) for x in range(256)])
good_chars = string.letters + string.digits
bad_chars = "".join([x for x in id_trans if x not in good_chars])
# then once for each maybe_bad_string
good_string = maybe_bad_string.translate(id_trans, bad_chars)

That is a bit over the top, but reading the doc to understand what is
going down will pay dividends.

(2) Here's a more straightforward way using the re module

# once
import re
subber = re.compile(r"[^A-Za-z0-9]").sub
# then once for each maybe_bad_string
good_string = subber("", maybe_bad_string)
# simpler but slower:
good_string = re.sub(r"[^A-Za-z0-9]", "", maybe_bad_string)
# and as a bonus extra
show_where_junk_was_string = subber("?", maybe_bad_string)

HTH,
John




More information about the Python-list mailing list