python - Replacing characters in a regex -
using python, have following strings:
['taxes............................. .7 21.4 (6.2)','regulatory , other matters..................$ 39.9 61.5 41.1','producer contract reformation cost recoveries............................ dash 26.3 28.3']
i need replace each of dots space, not periods in numbers. result should this:
['taxes .7 21.4 (6.2)','regulatory , other matters $ 39.9 61.5 41.1','producer contract reformation cost recoveries dash 26.3 28.3']
i've tried following:
dots=re.compile('(\.{2,})(\s*?[\d\(\$]|\s*?dash|\s*.)') newlist=[] each in list: newline=dots.sub(r'\2'.replace('.',' '),each) newdoc.append(newline)
but, code doesn't retain white space. thanks!
use negative lookarounds in re.sub
>>> import re >>> s = ['taxes............................. .7 21.4 (6.2)','regulatory , other matters..................$ 39.9 61.5 41.1','producer contract reformation cost recoveries............................ dash 26.3 28.3'] >>> [re.sub(r'(?<!\d)\.(?!\d)', ' ', i) in s] ['taxes .7 21.4 (6.2)', 'regulatory , other matters $ 39.9 61.5 41.1', 'producer contract reformation cost recoveries dash 26.3 28.3']
Comments
Post a Comment