[Tutor] line number when reading files using csv module

Kent Johnson kent37 at tds.net
Fri Oct 27 14:53:31 CEST 2006


Duncan Gibson wrote:
> 
> Kent Johnson wrote:
>> The line_num attribute is new in Python 2.5. This is a doc bug,
>> it should be noted in the description of line_num.
> 
> Is there some way to create a wrapper around a 2.4 csv.reader to
> give me pseudo line number handling? I've been experimenting with:
> 
>     import csv
> 
>     class MyReader(object):
>         
>         def __init__(self, inputFile):
>             self.reader = csv.reader(inputFile, delimiter=' ')
>             self.lineNumber = 0
> 
>         def __iter__(self):
>             self.lineNumber += 1
>             return self.reader.__iter__()

__iter__() should return self, not self.reader.__iter__(), otherwise 
Python is using the actual csv.reader not your wrapper. And don't 
increment line number here.

> 
>         def next(self):
>             self.lineNumber += 1        # do I need this one?

Yes.

>             return self.reader.next()

An easier way to do this is to use enumerate():
inputFile = file('data.csv', 'rb')
reader = csv.reader(inputFile)
for i, data in enumerate(reader):
   print i, data

Of course this will not necessarily give you the true line number, as a 
record may span multiple file lines and you may be processing header and 
blank lines at the start of the file before you get any records. But it 
is probably the best you can do and maybe it is what you need.

> 
>     if __name__ == '__main__':
>         inputFile = file('data.csv', 'rb')
>         reader = MyReader(inputFile)
>         for data in reader:
>             print reader.lineNumber, data
> 
> But that doesn't seem to do what I want. If I add some print statements
> to the methods, I can see that it calls __iter__ only once:

__iter__() will only be called once, it is next() that is called 
multiple times.

> 
>     __iter__
>     1 ['1', '2', '3']
>     1 ['2', '3', '4', '5']
>     1 ['3', '4', '5', '6', '7']
>     1 ['4', '5', '6', '7']
>     1 ['5', '6', '7', '8', '9']
> 
> Is there some other __special__ method that I need to forward to the
> csv.reader, or have I lost all control once __iter__ has done its job?

You lost control because you gave it away.

Kent

> 
> Cheers
> Duncan
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 




More information about the Tutor mailing list