Q:

Counting the number of lines (csv module)

I have a csv file and i want to transform it into a numerical dataset. To do so,i read every lines of the file and apply  a function that keeps what i want and print it in another csv file.
What I also want to do is to count the number of lines that I have read (number of lines in the original dataset)  and the number of errors that have occured (the original dataset has some bugs and my function will raise error)
Problem : With the following code I use (see below) returns  only half of the exact number of lines. Indeed, when I use it on a file with exactly 1 000 000 lines , nb_lines is only 500 000. And as I want to record the lines that are not "good", I guess that I must record the wrong lines:/
data=csv.reader(open(path1,"rb"),delimiter=';',skipinitialspace=True)
output=csv.writer(open(path2,"wb"))
error=csv.writer(open(path3,"wb"))
nb_error=0
nb_lines=0
for row in data:
    nb_lines=nbr_lines+1
    try:
        liste=data.next()
        toprint=function(liste)
        output.writerow(aprinter)
    except Exception as e:
        nb_error=nb_error+1
        badline=[nb_lines,e]
        error.writerow(badline)

What is wrong with my loop?
Thanks in advance :)

A:

You increment your iterator within the loop, for some reason. for row in data makes row the next line through each time. But then you do liste=data.next() - so you increment again. That means you skip every other line: it's not just your counter that is wrong, but you actually miss out half the data.
You should delete that line, and refer to row rather than liste within the loop.

