1

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!

2
  • did you try adding line_fix = sub('\..*?\.', '.', line) in the IF statement itself rather than writing it outside ? Commented Jan 3, 2018 at 14:19
  • Quick look would suggest you just need to indent the lines after the if so that they are only done when you find a line of interest, then append an else: f_out.write(line)) Commented Jan 3, 2018 at 14:19

1 Answer 1

2

Try this out. The only error I see is that portion which comes in the if condition. You are editing all lines of the file.

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)
            else:
                line_fix = line
            print('fixed: ' + line_fix)
            
            f_out.write(line.replace(line, line_fix))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.