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()