python - Deleting specific control characters(\n \r \t) from a string -
i have quite large amount of text include control charachters \n \t , \r. need replace them simple space--> " ". fastest way this? thanks
i think fastest way use str.translate()
:
import string s = "a\nb\rc\td" print s.translate(string.maketrans("\n\t\r", " "))
prints
a b c d
edit: once again turned discussion performance, here numbers. long strings, translate()
way faster using regular expressions:
s = "a\nb\rc\td " * 1250000 regex = re.compile(r'[\n\r\t]') %timeit t = regex.sub(" ", s) # 1 loops, best of 3: 1.19 s per loop table = string.maketrans("\n\t\r", " ") %timeit s.translate(table) # 10 loops, best of 3: 29.3 ms per loop
that's factor 40.
Comments
Post a Comment