Reading a CSV file

A

Ana Dionísio

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!
 
D

Dan Stromberg

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
 
A

Ana Dionísio

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
 
D

Dan Stromberg

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

#!/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
 
A

Ana Dionísio

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?
 
F

Fábio Santos

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.

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?
 
D

Dave Angel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top