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


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

Reading a CSV file

Started byAna Dionísio <anadionisio257@gmail.com>
First post2013-04-23 14:39 -0700
Last post2013-04-23 15:34 -0700
Articles 7 — 4 participants

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


Contents

  Reading a CSV file Ana Dionísio <anadionisio257@gmail.com> - 2013-04-23 14:39 -0700
    Re: Reading a CSV file Dan Stromberg <drsalists@gmail.com> - 2013-04-23 14:49 -0700
      Re: Reading a CSV file Ana Dionísio <anadionisio257@gmail.com> - 2013-04-23 14:58 -0700
        Re: Reading a CSV file Ana Dionísio <anadionisio257@gmail.com> - 2013-04-23 15:40 -0700
          Re: Reading a CSV file Fábio Santos <fabiosantosart@gmail.com> - 2013-04-24 00:30 +0100
          Re: Reading a CSV file Dave Angel <davea@davea.name> - 2013-04-23 23:18 -0400
        Re: Reading a CSV file Dan Stromberg <drsalists@gmail.com> - 2013-04-23 15:34 -0700

#44219 — Reading a CSV file

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-04-23 14:39 -0700
SubjectReading a CSV file
Message-ID<9d0d46e5-b579-4edc-be34-0aa8798b66f8@googlegroups.com>
Hello!

I need to read a CSV file that has "n" rows and "m" columns and if a certain condition is met, for exameple n==200, it prints all the columns in that row. How can I do this? I tried to save all the data in a multi-dimensional array but I get this error:

"ValueError: array is too big."

Thank you!

[toc] | [next] | [standalone]


#44223

FromDan Stromberg <drsalists@gmail.com>
Date2013-04-23 14:49 -0700
Message-ID<mailman.1000.1366753795.3114.python-list@python.org>
In reply to#44219

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

On Tue, Apr 23, 2013 at 2:39 PM, Ana Dionísio <anadionisio257@gmail.com>wrote:

> Hello!
>
> I need to read a CSV file that has "n" rows and "m" columns and if a
> certain condition is met, for exameple n==200, it prints all the columns in
> that row. How can I do this? I tried to save all the data in a
> multi-dimensional array but I get this error:
>
> "ValueError: array is too big


Use:
csv.reader(*csvfile*, *dialect='excel'*,
***fmtparams*)¶<http://docs.python.org/2/library/csv.html#csv.reader>

This will allow you to iterate over the values, instead of reading them all
into memory at once.

csv.reader is documented at:

http://docs.python.org/3/library/csv.html

http://docs.python.org/2/library/csv.html

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


#44225

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-04-23 14:58 -0700
Message-ID<665fa76f-3a19-414b-8524-c2a5bfa8b21b@googlegroups.com>
In reply to#44223
Thank you, but can you explain it a little better? I am just starting in python and I don't think I understood how to apply your awnser

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


#44228

FromAna Dionísio <anadionisio257@gmail.com>
Date2013-04-23 15:40 -0700
Message-ID<54f430e9-b0f0-42ab-86c3-37dfb3bc45bd@googlegroups.com>
In reply to#44225
The condition I want to meet is in the first column, so is there a way to read only the first column and if the condition is true, print the rest?

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


#44236

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-04-24 00:30 +0100
Message-ID<mailman.1007.1366760189.3114.python-list@python.org>
In reply to#44228

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

The enumerate function should allow you to check whether you are in the
first iteration.

Like so:

      for row_number, row in enumerate(csv.reader(<...>)):
          if enumerate == 0:
              if <your check...>:
                  break
          ...

Enumerate allows you to know how far into the iteration you are.

You could use the iterator's next() method too.

On 23 Apr 2013 23:53, "Ana Dionísio" <anadionisio257@gmail.com> wrote:
>
> The condition I want to meet is in the first column, so is there a way to
read only the first column and if the condition is true, print the rest?
> --
> http://mail.python.org/mailman/listinfo/python-list

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


#44242

FromDave Angel <davea@davea.name>
Date2013-04-23 23:18 -0400
Message-ID<mailman.1010.1366773516.3114.python-list@python.org>
In reply to#44228
On 04/23/2013 06:40 PM, Ana Dionísio wrote:
> The condition I want to meet is in the first column, so is there a way to read only the first column and if the condition is true, print the rest?
>

The CSV module will read a row at a time, but nothing gets printed till 
you print it.  So starting with Dan's code,

row[0] is column one of a given row, while row[1] is the next column, 
and so on.

import csv

def main():
     with open('test.csv', 'r') as file_:
         for row in csv.reader(file_, delimiter="|"):
             if row[0] == "special":
                 print row[1:]     #print columns starting at the second


-- 
DaveA

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


#44231

FromDan Stromberg <drsalists@gmail.com>
Date2013-04-23 15:34 -0700
Message-ID<mailman.1005.1366756987.3114.python-list@python.org>
In reply to#44225

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

On Tue, Apr 23, 2013 at 2:58 PM, Ana Dionísio <anadionisio257@gmail.com>wrote:

> Thank you, but can you explain it a little better? I am just starting in
> python and I don't think I understood how to apply your awnser
> --
> http://mail.python.org/mailman/listinfo/python-list
>

#!/usr/local/pypy-1.9/bin/pypy

import csv

def main():
    with open('test.csv', 'r') as file_:
        for row in csv.reader(file_, delimiter="|"):
            print row

main()

# Example input:
# abc|def|ghi
# jkl|mno|pqr

In this way, you get one row at a time, instead of all rows at once.

HTH

[toc] | [prev] | [standalone]


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


csiph-web