Using Python, I have the following strings:
['taxes............................. .7 21.4 (6.2)','regulatory and other matters..................$ 39.9 61.5 41.1','Producer contract reformation cost recoveries............................ DASH 26.3 28.3']
I need to replace each of the dots with a space, but not the periods in the numbers. So the result should look like this:
['taxes .7 21.4 (6.2)','regulatory and other matters $ 39.9 61.5 41.1','Producer contract reformation cost recoveries DASH 26.3 28.3']
I've tried the following:
dots=re.compile('(\.{2,})(\s*?[\d\(\$]|\s*?DASH|\s*.)')
newlist=[]
for each in list:
newline=dots.sub(r'\2'.replace('.',' '),each)
newdoc.append(newline)
But, this code doesn't retain the white space. Thanks!
Use negative lookarounds in re.sub
>>> import re
>>> s = ['taxes............................. .7 21.4 (6.2)','regulatory and other matters..................$ 39.9 61.5 41.1','Producer contract reformation cost recoveries............................ DASH 26.3 28.3']
>>> [re.sub(r'(?<!\d)\.(?!\d)', ' ', i) for i in s]
['taxes .7 21.4 (6.2)', 'regulatory and other matters $ 39.9 61.5 41.1', 'Producer contract reformation cost recoveries DASH 26.3 28.3']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With