quick and smart way for parsing a CSV file?
achrist at easystreet.com
achrist at easystreet.com
Wed Sep 3 17:02:02 EDT 2003
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