quick and smart way for parsing a CSV file?

Hank soundwave56 at yahoo.ca
Thu Sep 4 23:28:24 CEST 2003


where would i get this csv module from? does it come with python 2.2?

thanks


achrist at easystreet.com wrote in message news:<3F5656CA.519ABADF at easystreet.com>...
> Hank wrote:
> > 
> > Hi,
> > 
> > I have a CSV file from excel that looks like this (simplified):
> > 
> > name,description,type1,type2,name,filename
> > test1,this is a test,0.000,1.000,,
> > test2,another test,1.000,0.000,newname,filename
> > test3,this is a test,0.000,1.000,,
> > test4,this is a test,0.000,1.000,,
> > test5,this is a test,0.000,1.000,,
> > test6,this is a test,0.000,1.000,,
> > 
> > what i want at the end is a dictionary like
> > 
> > dict1[name] = test1
> > dict1[description] = "this is a test"
> > .
> > .
> > .
> > dict2[name] = test2
> > dict2[description] = "another test"
> > 
> 
> 
> import csv
> 
> dicts  		= []
> 
> inputFile = open("SomeDurnFileName.csv",  "rb")
> parser = csv.reader(inputFile)
> firstRec = True
> for fields in parser:
> 	if firstRec:
> 		fieldNames = fields
> 		firstRec   = False
> 	else:
> 		dicts.append({})
> 		for i,f in enumerate(fields)
> 			dicts[-1][fieldNames[i]] = f




More information about the Python-list mailing list