1

I have a text file and want to recursively replace all lines containing some regex pattern, then save the result into a new text file. The input text file has the following contents:


NAME1   184,743 184,439 14,305



NAME2   84,343  64,437  36,335




NAME3   154,543 174,439 38,385



I want to fill down all empty lines (including lines with only tabs and/or spaces) with the non-empty line above it. The final output should look like this:


NAME1   184,743 184,439 14,305
NAME1   184,743 184,439 14,305
NAME1   184,743 184,439 14,305
NAME1   184,743 184,439 14,305
NAME2   84,343  64,437  36,335
NAME2   84,343  64,437  36,335
NAME2   84,343  64,437  36,335
NAME2   84,343  64,437  36,335
NAME2   84,343  64,437  36,335
NAME3   154,543 174,439 38,385
NAME3   154,543 174,439 38,385
NAME3   154,543 174,439 38,385
NAME3   154,543 174,439 38,385

I tried this code but I can't figure how to make it work as I am new to Python. The regular expression works in Notepad++ but not in IDLE:

import re
fhand = open("/home/user1/Documents/inputtext.txt")
fout = open("/home/user1/Documents/outputtext.txt","w")

for line in fhand:
    re.sub("^(\S+.*)$(\n)^([\t ]+|)$","\1\2\1",line)
    fout.write(line)
fout.close()

1 Answer 1

1

You can use a simple loop that keeps track of the last line with any non-whitespace in it:

last = '\n'
for line in fhand:
    # if the line isn't empty after stripping all whitespaces
    if line.strip():
        # save this line into the variable last for later blank lines to copy from
        last = line
    # otherwise it's a blank line
    else:
        # and we should copy from the non-blank line saved in the variable last
        line = last
    fout.write(line)
fout.close()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, it works. The code looks "simple" but I just can't understand what's going on.
You're welcome. I've updated my code with explanations in the 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.