quick and smart way for parsing a CSV file?

H

Hank

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"

whats a fast and quick way of parsing it? Look for the first comma,
strip that entry out of the string, keep going? Thought there might be
some shortcuts.

Thanks
 
S

Skip Montanaro

soundwave56> I have a CSV file from excel that looks like this
soundwave56> (simplified):

soundwave56> name,description,type1,type2,name,filename
soundwave56> test1,this is a test,0.000,1.000,,
soundwave56> test2,another test,1.000,0.000,newname,filename
soundwave56> test3,this is a test,0.000,1.000,,
soundwave56> test4,this is a test,0.000,1.000,,
soundwave56> test5,this is a test,0.000,1.000,,
soundwave56> test6,this is a test,0.000,1.000,,

Take a look at the csv module delivered with Python 2.3.

Skip
 
A

achrist

Hank said:
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] = f
 
J

John Hunter

Hank> Hi, I have a CSV file from excel that looks like this
Hank> (simplified):

Hank> name,description,type1,type2,name,filename test1,this is a
Hank> test,0.000,1.000,, test2,another
Hank> test,1.000,0.000,newname,filename test3,this is a
Hank> test,0.000,1.000,, test4,this is a test,0.000,1.000,,
Hank> test5,this is a test,0.000,1.000,, test6,this is a
Hank> test,0.000,1.000,,

I posted some code recently to parse CSV into a dictionary where you
can extract rows or columns by key or index number. It's not exactly
what you asked for, but might help

http://groups.google.com/[email protected]&rnum=4

John Hunter
 
M

Michael Peuser

Hank said:
Hi,

I have a CSV file from excel that looks like this (simplified):
..........


import csv

dicts = []

inputFile = open("SomeDurnFileName.csv", "rb")
parser = csv.reader(inputFile)

If you do not use Python 2.3 for some reason or other there is a third party
CSV that is fast and workes fine with Python 2.2 (but uses a different
interface of course)
http://www.object-craft.com.au/projects/csv/

Kindly
Michael P
 
H

Hank

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

thanks


Hank said:
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] = f
 
S

Skip Montanaro

It comes with Python 2.3. If you grab the csv and _csv modules from the
Python 2.3 CVS repository, they should build and run under 2.2.x. If that's
not possible, poke around for Object Craft's csv module.

Skip
 
H

Hank

I'm using ActivePython2.2 which has the win32 extensions included.
Don't think they have a ActivePython2.3 out yet.

Also I was trying out PAGE which needed 2.2. Don't have the resources
to recompile for 2.3.

Thanks for the help!

Michael Peuser said:
Hank said:
Hi,

I have a CSV file from excel that looks like this (simplified):
.........


import csv

dicts = []

inputFile = open("SomeDurnFileName.csv", "rb")
parser = csv.reader(inputFile)

If you do not use Python 2.3 for some reason or other there is a third party
CSV that is fast and workes fine with Python 2.2 (but uses a different
interface of course)
http://www.object-craft.com.au/projects/csv/

Kindly
Michael P
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top