You can loop through the lines in a file like this:
with open('filename.txt') as f:
for line in f.readlines():
#do stuff
To take the information from a line that you want, you can do this:
cleaned = []
items = line.split()
for item in items:
if item.startswith('(') and item.endswith(')'):
cleaned.append(item.strip('()'))
break
cleaned.append(items[-1])
cleaned = ' '.join(cleaned)
Full program:
in_file = 'reach.txt'
out_file = 'outreach.txt'
def clean(string):
if not string:
return string
cleaned = []
items = string.split()
for item in items:
if item.startswith('(') and item.endswith(')'):
cleaned.append(item.strip('()'))
break
cleaned.append(items[-1])
return ' '.join(cleaned)
with open(in_file) as i, open(out_file, 'w') as o:
o.write('\n'.join([clean(line) for line in i]))