I have a large text file - sample:
EAST COAST-NORTH OF CAPE ST FRANCIS, NORTHEAST COAST-CAPE ST JOHN
AND SOUTH:
WNG: GALE.
WND: NW45 WITH G55 ALONG THE COAST. 01/12Z NW30. 01/18Z NE25.
VIS: 01/09Z-01/18Z 0-1 IN-PRECIP. 01/18Z-02/12Z 0-1 PTH-FG.
which I need to pop any line that starts with "AND SOUTH:" or "BANKS:", to the previous line. Yielding a result:
EAST COAST-NORTH OF CAPE ST FRANCIS, NORTHEAST COAST-CAPE ST JOHN AND SOUTH:
WNG: GALE.
WND: NW45 WITH G55 ALONG THE COAST. 01/12Z NW30. 01/18Z NE25.
VIS: 01/09Z-01/18Z 0-1 IN-PRECIP. 01/18Z-02/12Z 0-1 PTH-FG.
Here is the code I have written to deal with the issue thus far... Oddly,it sometimes works.
#Deals with pesky extra lines
lines = open(newfile,'r').readlines()
finalfile = open(final, 'w')
for i, line in enumerate(lines):
if line.startswith("AND SOUTH:") or line.startswith("BANKS:"):
lines[i-1] = lines[-1].strip() + line
lines.pop(i)
finalfile.write(line)