Reputation: 1075
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()
Upvotes: 1
Views: 75
Reputation: 106598
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()
Upvotes: 1