Tuesday, February 5, 2008

read CSV file in Python

Python has useful module named csv to deal with csv files. But recently I faced a problem reading csv files and want to share with you how I get rid of the problem. I wanted to get the data of the first column of my csv file. I tried the following code:

import csv

portfolio = csv.reader(open("portfolio.csv", "rb"))
names = []
for data in portfolio:
print names

but the output was an empty list ([]) :-(

Then I found the type of portfolio object using print type(portfolio) that the type is '_csv.reader', then I changed my program to the following and got the list :-)

import csv

portfolio = csv.reader(open("portfolio.csv", "rb"))
portfolio_list = []
names = []
for data in portfolio_list:
print names

If you have any better idea, please share.

To know more about csv module, http://docs.python.org/lib/module-csv.html


azalea said...

portfolio = csv.reader(open("portfolio.csv", "rU"))

It should work

Install & Configuration of TRANSFAC for Windows said...

It really Helped me. Thank you

Install & Configuration of TRANSFAC for Windows said...

Reading a particular column from a csv file is really useful and worthful. In my project, I also need to write an array of elements into a new column in the existing csv file in Python. Can you please post some information for writing column data into csv file in Python.

Thank you,

subeen said...

I think if you want to write data in a new column in an existing CSV file, you have to read the whole file, then write it with the extra column. I don't know any other alternative.

WJ said...

thanx that was helpful for me as well, i used it to create a function that reads a (excel based) CSV file and then return it as a list of tuples.

this list can then be ordered in antoher part of the program.

Mind that these are my very first lines of code ever, so sorry probably i missed some good coding conventions and such, anyway the code looks like this:

def csvimport(filename='book2.csv'):
#import cvslist containing bids of all players and convert it to a list of tuples, namely the bidlist
import csv
#open filename
bidlist = []
for row in bidfile:
#Append row as tuble to larger bidlist
for i in range(len(row)):
#convert strings in file to tuple of floats and/or intigers
except ValueError:
return bidlist

Diego said...

Simple solution:

list = [ x[0] for x in cvs.reader(open('x.csv'),'r') ]

Guenael said...

Diego: you rock
but the parenthesis is in the wrong spot:
list = [ x[0] for x in cvs.reader(open('x.csv','r')) ]

Guenael said...

And who's a moron? I left the "cvs" typo. Another try:

import csv
list = [ x[0] for x in csv.reader(open('x.csv','r')) ]

handofdoom said...

No need for a comprehension. list() will cast the iterator to a list:
list = list(portfolio)

In a one-liner:
portfolio = list(csv.reader(open("portfolio.csv", "r")))

blair said...

class readInCSV:
def __init__(self, fileName):
#instantiate the CSV file variable:
self.fileName = fileName
#read the CSV file and open it:
self.fileReader = csv.reader(open(self.fileName, "rb"), delimiter = ',')
#create a local array to hold the CSV data:
self.fileReaderList = []
#pop the file data into the local array:
for data in self.fileReader:

The only problem with your code was you put data[0], which will only give you the first element of the array. Using data, you get the full list.

kishan shetty said...

You can directly use row = resource.readline().split(','), this will gives the whole line as a single list. To get the first column by indexing row[0]. You have to repeat the about until end of file is reached.

For more info and direct codes use this link this will helps you.