regex - Python - Replace parenthesis with periods and remove first and last period -


i trying take input file list of dns lookups contains subdomain/domain separators string length in parenthesis opposed periods. looks this:

(8)subdomain(5)domain(3)com(0) (8)subdomain(5)domain(3)com(0) (8)subdomain(5)domain(3)com(0) 

i replace parenthesis , numbers periods , remove first , last period. code this, leaves last period. appreciated. here code:

import re  file = open('test.txt', 'rb') writer = open('outfile.txt', 'wb')   line in file:     newline1 = re.sub(r"\(\d+\)",".",line)       if newline1.startswith('.'):         newline1 = newline1[1:-1]         writer.write(newline1) 

you can split lines \(\d+\) regex , join . stripping commas @ both ends:

for line in file:     res =".".join(re.split(r'\(\d+\)', line))     writer.write(res.strip('.')) 

see ideone demo


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -