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
Post a Comment