This class replaces characters until there are no offending ones left.
You could you regex or translate. This is a method using dictionaries and replace method from strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import string
class multi_replace:
def __init__(self,keep=[],debug=''):
self.keep={};self.debug=0
if debug: self.debug=1
if keep:
for i in keep: self.keep[i]=1
else:
#keep all keyboard characters plus whitespace
for i in string.printable: self.keep[i]=1
#remove space characters except for ' '
for i in string.whitespace:
if i==' ': continue #whitespace is valid
if i in self.keep: del self.keep[i]
def replace(self,line,new_char):
bad=0;changed=0
#keep on replacing until there are no bad characters left
while 1:
clean=1
for c in line:
if c not in self.keep:
clean=0
line=line.replace(c,new_char)
changed=1
#cannot continue replacing since line has changed w/for loop
break
#break the while loop if nothing bad was found
if clean: break
if self.debug and changed: print line
return line
|
This is good for filtering logfiles. Here is an example.
by default only accept keyboard chars and ' '
you can also provide a list of chars
m=multi_replace() f=open('e.log') for line in f: line=line.strip() #replace all unacceptable chars with # line=m.replace(line,'#')
If you want to print to the screen every time it changes a line, set debug=1 m=multi_replace(debug=1)