if I have a text file like this:
[001]This is line 1.
[002][too long]This is line 2 but it's Tooooo
oooo long!
[003]This is line 3.
I write a ‘for line in fileA’ to read this file like:
for line in fileA:
...
now I need to merge the current line and the next line when line.find(“[too long]”)>=0.
How should I do?
PS:
I wrote:
for line in fileA:
if line.find("[too long]")>=0:
loc = fileA.tell()
fileB = open("file.txt") #open this file again
fileB.seek(loc)
line += fileB.readline().strip()
but it did not work. why?
Sounds too much overhead with extra reading of the file. Try this:
prints
This appends the following line if
[too long]is found in a line. Maybe you want to append all further lines until a line starts with something like[xxx]?