ascii character - removing chars from string
bearophileHUGS at lycos.com
bearophileHUGS at lycos.com
Mon Jul 3 19:45:12 EDT 2006
bruce:
> valid_str = strip(invalid_str)
> where 'strip' removes/strips out the invalid chars...
This isn't short but it is fast:
import string
valid_chars = string.lowercase + string.uppercase + \
string.digits +
"""|!'\\"£$%&/()=?^*é§_:;>+,.-<\n \t"""
all_chars = "".join(map( chr, range(256)) )
comp_valid_chars = "".join( set(all_chars).difference(valid_chars) )
print "test string".translate(all_chars, comp_valid_chars)
Shorter and a bit slower alternative:
import string
valid_chars_set = set(string.lowercase + string.uppercase
+ string.digits +
"""|!'\\"£$%&/()=?^*é§_:;>+,.-<\n \t""")
print filter(lambda c: c in valid_chars_set, "test string")
You can add the chars you want to the string of accepted ones.
Bye,
bearophile
More information about the Python-list
mailing list