I have this assignment where I write certain lines from an input file. These lines I'm looking for in this case is only identified by its following line ("EOL"). It goes like (shortened):

while True:
     line = infile.readlines()
     if not line:
           break

     linesplit = line.split(",")
     line = line[:-1]

     if line startswith("Line"):
           write stuff
     if line startswith("$GPGGA"):
          calculate and write stuff
     if line startswith("EOL")
          outfile.write(i really need the line previous to this one here somehow)
          outfile.write(line + "\n")

Recommended Answers

All 3 Replies

the closest i've come to anything is to get out at which byte the "EOL" line is, but i can't from that tell it to print from byte x to byte y. I do know the length of each line.

You're making it too difficult. Store the previous record in memory.

prev_line = ""
for line in infile:
     linesplit = line.split(",")
     line = line[:-1]

     if line startswith("Line"):
           write stuff
     if line startswith("$GPGGA"):
          calculate and write stuff
     if line startswith("EOL")
          outfile.write("%s\n" % (prev_line))   <==========
     ##  copying = last thing in this loop
     prev_line = line                              <==========

wow. thanks alot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.