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:
    names.append(data[0])
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 = []
portfolio_list.extend(portfolio)
names = []
for data in portfolio_list:
    names.append(data[0])
print names


If you have any better idea, please share.

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

Comments

azalea said…
Try
portfolio = csv.reader(open("portfolio.csv", "rU"))

It should work
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,
Suprabhath
tejsupra@gmail.com
Tamim Shahriar 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.
Unknown 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
filename='book2.csv'
bidfile=csv.reader(open(filename,"rU"),dialect='excel',delimiter=";")
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
try:
t=int(row[i])
row[i]=t
except ValueError:
t=float(row[i])
row[i]=t
row=tuple(row)
bidlist.append(row)
return bidlist
Unknown said…
Simple solution:

list = [ x[0] for x in cvs.reader(open('x.csv'),'r') ]
Unknown said…
Diego: you rock
but the parenthesis is in the wrong spot:
list = [ x[0] for x in cvs.reader(open('x.csv','r')) ]
Unknown 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')) ]
Anonymous 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")))
Unknown 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:
self.fileReaderList.append(data)


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.
Unknown 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.
http://articlesdictionary.wordpress.com/2013/09/29/read-csv-file-in-python/

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code