Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Number of cells, using CSV module

17 views
Skip to first unread message

tuna...@gmail.com

unread,
May 16, 2013, 2:29:25 PM5/16/13
to
I'm using the csv module to get information from a csv file. I have items listed in Column A. I want to know how many items are listed in Column A.

import csv
with open('test.csv', 'r') as f:
reader = csv.reader(f)
for column in reader:
column = (column[0])
print(column)

We are given

>a
>b
>c
>d
>e
>f

How would I go about getting the amount of numbers that are printed? If I try printing len(column), I get
>1
>1
>1
>1
>1
>1

Skip Montanaro

unread,
May 16, 2013, 2:40:08 PM5/16/13
to tuna...@gmail.com, pytho...@python.org
Perhaps you want len(reader) instead?  Or a counter which increments for every row read which has an item in column A?

Skip

tuna...@gmail.com

unread,
May 16, 2013, 2:50:42 PM5/16/13
to
On Thursday, May 16, 2013 2:40:08 PM UTC-4, Skip Montanaro wrote:
> Perhaps you want len(reader) instead?  Or a counter which increments for every row read which has an item in column A?
>
>
>
> Skip

len(reader) gives me an error.

I tried a counter, but unfortunately due to the simplicity of CSV files, any formulas are not saved.

tuna...@gmail.com

unread,
May 16, 2013, 2:53:23 PM5/16/13
to
I guess another way to accomplish this would be, is there any way that I can turn the returned value for (column) into 1 list?

So rather than
>a
>b
>c
>d
>e
>f
I would get [a, b, c, d, e, f]

Skip Montanaro

unread,
May 16, 2013, 3:07:49 PM5/16/13
to tuna...@gmail.com, pytho...@python.org
> len(reader) gives me an error.

Apologies. len(list(reader)) should work. Of course, you'll wind up
loading the entire CSV file into memory. You might want to just count
row-by-row:

n = 0
for row in reader:
n += 1

Skip

Skip Montanaro

unread,
May 16, 2013, 3:08:29 PM5/16/13
to tuna...@gmail.com, pytho...@python.org
> So rather than
>>a
>>b
>>c
>>d
>>e
>>f
> I would get [a, b, c, d, e, f]

all_items = []
for row in reader:
all_items.append(row[0])

Skip

Tim Chase

unread,
May 16, 2013, 4:00:21 PM5/16/13
to Skip Montanaro, tuna...@gmail.com, pytho...@python.org
On 2013-05-16 14:07, Skip Montanaro wrote:
> > len(reader) gives me an error.
>
> Apologies. len(list(reader)) should work. Of course, you'll wind
> up loading the entire CSV file into memory. You might want to just
> count row-by-row:
>
> n = 0
> for row in reader:
> n += 1

which can nicely be rewritten as

n = sum(1 for row in reader)

:-)

-tkc


Tim Chase

unread,
May 16, 2013, 4:01:08 PM5/16/13
to Skip Montanaro, tuna...@gmail.com, pytho...@python.org
On 2013-05-16 14:08, Skip Montanaro wrote:
> > So rather than
> >>a
> >>b
> >>c
> >>d
> >>e
> >>f
> > I would get [a, b, c, d, e, f]
>
> all_items = []
> for row in reader:
> all_items.append(row[0])

And following up here, this could be tidily rewritten as

all_items = [row[0] for row in reader]

-tkc



tuna...@gmail.com

unread,
May 16, 2013, 4:11:50 PM5/16/13
to
Thank you Skip, worked great. And thank you Tim for Tidying things up!
0 new messages