[Tutor] clean text
Alan Gauld
alan.gauld at btinternet.com
Tue May 19 20:36:39 CEST 2009
"spir" <denis.spir at free.fr> wrote
> def _cleanRepr(text):
> ''' text with control chars replaced by repr() equivalent '''
> result = ""
> for char in text:
> n = ord(char)
> if (n < 32) or (n > 126 and n < 160):
> char = repr(char)[1:-1]
> result += char
> return result
I haven't read the rest of the responses yet but my first
suggestion is to invert the process and use replace()
Instead of looping through every char in the string search
for the illegal chars. A regex should enable you to do
a single findall search. If you find any you can loop
over the instances using replace() or sub() to change
them. The translate module might even let you do all
changes in one go.
That should be faster I think.
HTH
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list