Reading a CSV file

Dave Angel davea at davea.name
Tue Apr 23 23:18:13 EDT 2013


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



More information about the Python-list mailing list