Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > quick and smart way for parsing a CSV file?

Reply
Thread Tools

quick and smart way for parsing a CSV file?

 
 
Hank
Guest
Posts: n/a
 
      09-03-2003
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
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      09-03-2003

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

 
Reply With Quote
 
 
 
 
achrist@easystreet.com
Guest
Posts: n/a
 
      09-03-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
 
Reply With Quote
 
John Hunter
Guest
Posts: n/a
 
      09-04-2003
>>>>> "Hank" == Hank <(E-Mail Removed)> writes:

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/groups?hl=e...hon.org&rnum=4

John Hunter

 
Reply With Quote
 
Michael Peuser
Guest
Posts: n/a
 
      09-04-2003

<(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Hank wrote:
> >
> > 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



 
Reply With Quote
 
Hank
Guest
Posts: n/a
 
      09-04-2003
where would i get this csv module from? does it come with python 2.2?

thanks


http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in message news:<(E-Mail Removed)>...
> 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

 
Reply With Quote
 
Skip Montanaro
Guest
Posts: n/a
 
      09-04-2003

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


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

 
Reply With Quote
 
Michael Peuser
Guest
Posts: n/a
 
      09-04-2003

"Hank" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) om...
> where would i get this csv module from? does it come with python 2.2?
>
> thanks


No of course - read my posting!

Kindly
Michael P


 
Reply With Quote
 
Hank
Guest
Posts: n/a
 
      09-05-2003
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" <(E-Mail Removed)> wrote in message news:<bj85d3$iav$03$(E-Mail Removed)-online.com>...
> <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
> > Hank wrote:
> > >
> > > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to parse a csv...... a csv that has CRLF in the fields sso Java 20 04-26-2009 11:04 AM
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Smart Card Certificate Logon and Smart Card Wireless EAP-TLS erha Wireless Networking 0 05-19-2005 01:40 AM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM
trade 64mb smart media for 16mb smart media cards wjva Digital Photography 1 08-20-2003 08:30 PM



Advertisments