user1178682
user1178682

Reputation:

File iteration, checking line existence

I am reading through a file using a for loop like this...

f = open("somefile.txt")

for line in f:
    do stuff

except for each line I read i need to take an item from the line ahead of it and put it in the current line. What is the best way to do this? Is there a way to read the next line or get some item from it without reading it?

Upvotes: 6

Views: 11753

Answers (4)

Gareth Latty
Gareth Latty

Reputation: 89027

If my understanding is correct, and you want to work on each line in turn, using some value from the next line, my suggestion would be simply to store the value you are currently reading, and work on the last value. Work in reverse - the last_line is your current line and line is the next one.

last_line = None

with open("somefile.txt") as f:
    for line in f:
        if not last_line == None:
            do_stuff(last_line, extract_needed_part(line))
        last_line = line
do_stuff(last_line) #The final line without anything following it.

In mathematical terms, instead of line n and line n+1, do line n-1 and line n. Same effect.

The upside to this method is it doesn't mean loading the entire file at the beginning.

Upvotes: 7

secretmike
secretmike

Reputation: 9884

If your file is not huge, you can read it into memory and use it there:

f = open("somefile.txt")
lines = f.readlines()
f.close()

for index, value in enumerate(lines):
    # Check if next line exists
    if index + 1 > len(lines):
        next_line = lines(index + 1)
        # do something with line and next_line

Edit:

For large files, it would be easiest to just remember your previous line:

f = open("somefile.txt")
previous_line = f.readline()
for line in f:
    # Do something with line and previous_line
    print(line, previous_line)
    # Save this line for the next iteration
    previous_line = line

There might be edge cases depending on what your data looks like. Do your lines always come in pairs or do you just need a bit of info from the next line in certain circumstances.

For instance this code won't do anything if your file only has one line.

Upvotes: 3

eyquem
eyquem

Reputation: 27585

with open('somefile.txt') as f, open('somefile.txt') as g:
    g.readline()
    lines = ( (f.readline(),line) for line in g)
        for precline,aheadline in lines:
            # do what you want

Upvotes: 0

jcollado
jcollado

Reputation: 40394

If your file fits in memory, you can try something like this:

f = open('somefile.txt')
lines = f.read().splitlines()

for current_line, next_line in zip(lines, lines[1:]):
    print current_line
    print next_line
    print '-------'

The code above basically reads all the lines and uses zip to create a list of tuples that contains the current line and the next one.

Edit: Alternatively, for long files, you can use itertools library as follows:

import itertools
f = open('somefile.txt')
i1, i2 = itertools.tee(f)
lines = itertools.izip(i1, itertools.islice(i2, 1, None))
for current_line, next_line in lines:
    print current_line
    print next_line
    print '-------'

In this case:

  • itertools.tee is used to create two indenpendent iterators (one for the current line and one for the next line) that use the original file iterator.
  • itertools.slice is used to start the next line iterator in the second line.
  • itertools.izip is used to join the results of both iterators line by line in a tuple.

Edit 2: As suggested by @eyquem, you can also open the file twice:

import itertools
f = open('somefile.txt')
g = open('somefile.txt')
lines = itertools.izip(f, itertools.islice(g, 1, None))
for current_line, next_line in lines:
    print current_line
    print next_line
    print '-------'

Upvotes: 0

Related Questions