I'm trying to go through a text file and find all the lines that start with 'file='. When I find these lines I want to remove all the text in between the dots and finally save that as a new file.
Such lines look like so
file="image.&!145.jpg"
I'm currently stuck to the place where I either:
- delete the text between dots all over the file (not just the lines that start with 'file=')
- delete the text between the dots only on the lines that start with 'file=', but I only end up saving that single line in the new document.
Here's the code that I have so far:
import os
from os.path import basename
from re import sub
file_in = 'tex_file_01.txt'
file_out = 'tex_file_01_new.txt'
with open(file_in, 'r') as f_in:
with open(file_out, 'w') as f_out:
for line in f_in:
if 'file=' in line:
print 'found: ' + line
line_fix = sub('\..*?\.', '.', line)
print 'fixed: ' + line_fix
f_out.write(line.replace(line, line_fix))
The above piece of code deletes the text between the dots all over the file.
Any ideas? Thanks in advance!
line_fix = sub('\..*?\.', '.', line)in theIFstatement itself rather than writing it outside ?ifso that they are only done when you find a line of interest, then append anelse: f_out.write(line))