3

I want to loop through a csv document and often check the next row for values, but stick to the same row number. The .next() is skipping a row, which won't work for me, unless I could go back again somehow. Is there a simple way in doing this? I tried the pairwise/cycle but it takes way too long if the file is big. My sample code is:

file1 = open("name.csv", 'rb')
reader = csv.DictReader(file1)
new_rows_list = []

for row in reader:
    if row['IsFixated'] == 'TRUE':
        new_row = [row['Result'], 'Fixation']
        new_rows_list.append(new_row)
    else:
        new_row = [row['Result'], 'Saccade']
        new_rows_list.append(new_row)
        nextRow = reader.next() # check for next row ??
        if nextRow['IsFixated'] == 'TRUE':
            new_rows_list = CheckPreviousPositions(new_rows_list)

file1.close()

2 Answers 2

2
reader1,reader2 = itertools.tee(csv.DictReader(file1))
#this creates two copies of file iterators for the file
next(reader2) #skip first line in second filehandle
for line,next_line in itertools.izip(reader1,reader2):
    #do something?

I guess ... maybe?

Sign up to request clarification or add additional context in comments.

Comments

0

If you only need to check the next line this generator function should do the trick

def next_reader(file_name):
    with open(file_name) as f:
        reader = csv.reader(f)
        check = 0
        curr_and_next = []
        while True:
            if check == 0:
                first = reader.next()
                second = reader.next()
                curr_and_next = [first, second]
                check = 1
            else:
                curr_and_next = [curr_and_next[1], reader.next()]
            yield curr_and_next

This opens up your file and add the first and second line to the curr_and_next list and returns it. In any subsequent call it will move the second value in curr_and_next into the first spot and then add the next line in the file into the second spot. Now instead of yielding one row at a time, this generator will yield a list containing the next row and the row following it. For example if you have a csv file alpha.csv with the rows:

a,b
c,d
e,f

and you have a loop:

for x in next_reader('alpha.csv'):
    print x

this will print:

[['a','b'],['c','d']]
[['c','d'],['e','f']]

Note that the last line in your csv file will only appear once in the very last iteration. (You won't get something like [['e','f'],None] for the last print).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.