Looping through File Question
Ricardo Aráoz
ricaraoz at gmail.com
Thu Sep 6 10:50:21 EDT 2007
John Machin wrote:
> On Sep 5, 10:26 pm, planetmatt <planetm... at gmail.com> wrote:
>> On 5 Sep, 12:34, John Machin <sjmac... at lexicon.net> wrote:
>>
>>
>>
>>> On Sep 5, 8:58 pm, planetmatt <planetm... at gmail.com> wrote:
>>>> I am a Python beginner. I am trying to loop through a CSV file which
>>>> I can do. What I want to change though is for the loop to start at
>>>> row 2 in the file thus excluding column headers.
>>>> At present I am using this statement to initiate a loop though the
>>>> records:
>>>> for line in f.readlines():
>>>> How do I start this at row 2?
>>> The quick answer to your literal question is:
>>> for line in f.readlines()[1:]:
>>> or, with extreme loss of elegance, this:
>>> for lino, line in enumerate(f.readlines()):
>>> if not lino:
>>> continue
>>> But readline and readlines are old hat, and you wouldn't want to read
>>> a file of a few million lines into a big list, so a better answer is:
>>> _unused = f.next()
>>> for line in f:
>>> But you did say you were reading a CSV file, and you don't really want
>>> to do your own CSV parsing, even if you think you know how to get it
>>> right, so best is:
>>> import csv
>>> rdr = csv.reader(f)
>>> heading_row = rdr.next()
>>> for data_row in rdr:
>>> HTH,
>>> John
>> Thanks so much for the quick response. All working now.
>>
>> I had looked at the CSV module but when I ran into another problem of
>> trying to loop through all columns, I was given a solution in another
>> forum which used the readlines() method.
>
> Antique advice which still left you doing the CSV parsing :-)
>
>> I have looked at the CSV documentation but didn't see any mention of
>> heading_row or data_row.
>
> Ummm ... 'heading_row' and 'data_row' are identifiers of the kind that
> you or I would need to make up in any language; why did you expect to
> find them in the documentation?
>
>> Is there a definitive Python documentation
>> site with code examples like MS's MSDN?
>
> The definitive Python documentation site is (I suppose) http://www.python.org/doc/
>
> I don't know what "code examples like MS's MSDN" means. I avoid MSDN
> like I'd avoid a nurse carrying a bottle of Dettol and a wire
> brush :-)
>
> Here's an example of sucking your data into a list of lists and
> accessing it columnwise:
>
> rdr = csv.reader(f)
> whatever_you_want_to_call_the_heading_row = rdr.next()
> data = list(rdr)
> sum_col_5 = sum(float(row[5]) for row in data)
> print "The value in row 3, column 4 is", data[3][4]
>
> HTH,
> John
>
Given that you have a header row you could also do (untested):
rdr = csv.DictReader(f)
data = list(rdr)
sum_Value = sum(float(row['Value']) for row in data)
print "The value in row 3, column 'ColName' is", data[3]['ColName']
DictReader generates a list of dictionaries which take the keys from
your header row.
More information about the Python-list
mailing list