Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45445 > unrolled thread

Number of cells, using CSV module

Started bytunacubes@gmail.com
First post2013-05-16 11:29 -0700
Last post2013-05-16 13:11 -0700
Articles 9 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Number of cells, using CSV module tunacubes@gmail.com - 2013-05-16 11:29 -0700
    Re: Number of cells, using CSV module Skip Montanaro <skip@pobox.com> - 2013-05-16 13:40 -0500
      Re: Number of cells, using CSV module tunacubes@gmail.com - 2013-05-16 11:50 -0700
        Re: Number of cells, using CSV module Skip Montanaro <skip@pobox.com> - 2013-05-16 14:07 -0500
        Re: Number of cells, using CSV module Tim Chase <python.list@tim.thechases.com> - 2013-05-16 15:00 -0500
    Re: Number of cells, using CSV module tunacubes@gmail.com - 2013-05-16 11:53 -0700
      Re: Number of cells, using CSV module Skip Montanaro <skip@pobox.com> - 2013-05-16 14:08 -0500
      Re: Number of cells, using CSV module Tim Chase <python.list@tim.thechases.com> - 2013-05-16 15:01 -0500
    Re: Number of cells, using CSV module tunacubes@gmail.com - 2013-05-16 13:11 -0700

#45445 — Number of cells, using CSV module

Fromtunacubes@gmail.com
Date2013-05-16 11:29 -0700
SubjectNumber of cells, using CSV module
Message-ID<5f4aec0e-5cfa-445f-af50-67cca7caae36@googlegroups.com>
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

[toc] | [next] | [standalone]


#45447

FromSkip Montanaro <skip@pobox.com>
Date2013-05-16 13:40 -0500
Message-ID<mailman.1769.1368729617.3114.python-list@python.org>
In reply to#45445

[Multipart message — attachments visible in raw view] — view raw

Perhaps you want len(reader) instead?  Or a counter which increments for
every row read which has an item in column A?

Skip

[toc] | [prev] | [next] | [standalone]


#45448

Fromtunacubes@gmail.com
Date2013-05-16 11:50 -0700
Message-ID<8662be9c-e50c-48e9-86dc-1838c6188d0c@googlegroups.com>
In reply to#45447
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.

[toc] | [prev] | [next] | [standalone]


#45450

FromSkip Montanaro <skip@pobox.com>
Date2013-05-16 14:07 -0500
Message-ID<mailman.1770.1368731278.3114.python-list@python.org>
In reply to#45448
> 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

[toc] | [prev] | [next] | [standalone]


#45454

FromTim Chase <python.list@tim.thechases.com>
Date2013-05-16 15:00 -0500
Message-ID<mailman.1775.1368734307.3114.python-list@python.org>
In reply to#45448
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

[toc] | [prev] | [next] | [standalone]


#45449

Fromtunacubes@gmail.com
Date2013-05-16 11:53 -0700
Message-ID<2cd2fe5b-a242-442e-a3fa-e0c5588a370a@googlegroups.com>
In reply to#45445
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]

[toc] | [prev] | [next] | [standalone]


#45451

FromSkip Montanaro <skip@pobox.com>
Date2013-05-16 14:08 -0500
Message-ID<mailman.1771.1368731318.3114.python-list@python.org>
In reply to#45449
> 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

[toc] | [prev] | [next] | [standalone]


#45455

FromTim Chase <python.list@tim.thechases.com>
Date2013-05-16 15:01 -0500
Message-ID<mailman.1776.1368734348.3114.python-list@python.org>
In reply to#45449
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


[toc] | [prev] | [next] | [standalone]


#45457

Fromtunacubes@gmail.com
Date2013-05-16 13:11 -0700
Message-ID<28454e0b-b14d-4a71-9ac4-38e7397f44d7@googlegroups.com>
In reply to#45445
Thank you Skip, worked great. And thank you Tim for Tidying things up!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web