954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Error reading CSV file (absolute beginner)

I use the following code to read a CSV file with 6 columns. The headers are assigned correctly (i.e. print headers works) but when I try to read the rest of the rows as variables under those headers. The first column is 'time' and it is read correctly. But when I replace 'time' in the print code with other header names I get an error saying for ex., "name v_bot is not defined"-- v_bot is my third column. All the columns have numerical values.
Does anything look glaringly wrong here? I am an absolute newbie so I won't be surprised if it is something stupid. Thanks in advance!

# Read the header row
ifile= open(filename, 'rb')
reader = csv.reader(ifile, dialect='excel')
headers = reader.next()
ifile.close()

# Read the whole file
fin = list(csv.reader(open(filename,'rb')))
alldata = fin[1:len(fin)]
print headers
for col in range(0,len(headers)):
                vars()[headers[col]]=[float(x[col]) for x in alldata]

print time[0:10]
new_pythonuser
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: