Scan CSV file and saving it into an array

Rhodri James rhodri at wildebst.demon.co.uk
Wed Apr 24 19:21:18 EDT 2013


On Thu, 25 Apr 2013 00:01:01 +0100, Ana Dionísio  
<anadionisio257 at gmail.com> wrote:

> I tried to do for row in len(10): but I get an error.

Please remember to tell us what error, with the traceback, so that we can  
make fun of you for not reading it :-)  In this case it's pretty obvious;  
Python will complain that you can't call len() on an integer (because it's  
not a container type, so the whole idea of "length" is meaningless).  You  
probably meant to use "range(10)" instead of "len(10)", but then "row"  
would be the integers from 0 to 9 inclusive, which isn't what you want  
either.

What you actually want to do is "row = p.next()" (in Python 2.x) or "row =  
next(p)" (in Python 3.x) to get the next row from the CSV file, add it to  
the array and repeat until you have ten more lines.  Another for-loop will  
make this less tedious to write.

Odd that this subject should have come up so many times in various guises  
in the last week or two.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list