Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Reading a CSV file into a list of dictionaries
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
RFQ  
View profile  
 More options Jun 6 2005, 1:13 pm
Newsgroups: comp.lang.python
From: "RFQ" <rfque...@gmail.com>
Date: 6 Jun 2005 10:13:58 -0700
Local: Mon, Jun 6 2005 1:13 pm
Subject: Reading a CSV file into a list of dictionaries
Hi, I'm struggling here to do the following with any success:

I have a comma delimited file where each line in the file is something
like:

PNumber,3056,Contractor,XYZ Contracting,Architect,ABC Architects,...

So each line is intended to be: key1,value1,key2,value2,key3,value3...
and each line is to be variable in length (although it will have to be
an even number of records so that each key has a value).

I want to read in this csv file and parse it into a list of
dictionaries. So each record in the list is a dictionary:

{"PNumber":"3056","Contractor":"XYZ Contracting", ... }

I have no problem reading in the CSV file to a list and splitting each
line in the file into its comma separated values. But I can't figure
out how to parse each resulting list into a dictionary.

Any help on this?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Otten  
View profile  
 More options Jun 6 2005, 1:34 pm
Newsgroups: comp.lang.python
From: Peter Otten <__pete...@web.de>
Date: Mon, 06 Jun 2005 19:34:24 +0200
Local: Mon, Jun 6 2005 1:34 pm
Subject: Re: Reading a CSV file into a list of dictionaries

RFQ wrote:
> I have a comma delimited file where each line in the file is something
> like:

> PNumber,3056,Contractor,XYZ Contracting,Architect,ABC Architects,...

> So each line is intended to be: key1,value1,key2,value2,key3,value3...
> and each line is to be variable in length (although it will have to be
> an even number of records so that each key has a value).

> I want to read in this csv file and parse it into a list of
> dictionaries. So each record in the list is a dictionary:

> {"PNumber":"3056","Contractor":"XYZ Contracting", ... }
>>> row

['PNumber', '3056', 'Contractor', 'XYZ Contracting', 'Architect', 'ABC']
>>> dict(zip(row[::2], row[1::2]))

{'Architect': 'ABC', 'PNumber': '3056', 'Contractor': 'XYZ Contracting'}

A bit more elegant:

>>> irow = iter(row)
>>> dict(zip(irow, irow))

{'Architect': 'ABC', 'PNumber': '3056', 'Contractor': 'XYZ Contracting'}

Peter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Kern  
View profile  
 More options Jun 6 2005, 1:28 pm
Newsgroups: comp.lang.python
From: Robert Kern <rk...@ucsd.edu>
Date: Mon, 06 Jun 2005 10:28:00 -0700
Subject: Re: Reading a CSV file into a list of dictionaries

First, don't process the CSV stuff yourself. Use the csv module.

In [9]:import csv

In [10]:f = open('foo.csv')

In [11]:cr = csv.reader(f)

In [12]:for row in cr:
    ....:    print dict(zip(row[::2], row[1::2]))
    ....:
{'Architect': 'ABC Architects', 'PNumber': '3056', 'Contractor': 'XYZ
Contracting'}
{'Architect': 'ABC Architects', 'PNumber': '3056', 'Contractor': 'XYZ
Contracting'}
[etc.]

--
Robert Kern
rk...@ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
GMane Python  
View profile  
 More options Jun 7 2005, 12:34 pm
Newsgroups: comp.lang.python
From: "GMane Python" <s_david_r...@hotmail.com>
Date: Tue, 7 Jun 2005 12:34:08 -0400
Local: Tues, Jun 7 2005 12:34 pm
Subject: Re: Reading a CSV file into a list of dictionaries
Sounds like you want to use the ConfigObject module.

http://www.voidspace.org.uk/python/modules.shtml#configobj

-dave

"RFQ" <rfque...@gmail.com> wrote in message

news:1118078038.100136.56630@f14g2000cwb.googlegroups.com...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Laurent RAHUEL  
View profile  
 More options Jun 7 2005, 3:57 pm
Newsgroups: comp.lang.python
From: Laurent RAHUEL <laurent.rah...@net-ng.com>
Date: Tue, 07 Jun 2005 21:57:24 +0200
Local: Tues, Jun 7 2005 3:57 pm
Subject: Re: Reading a CSV file into a list of dictionaries

RFQ wrote:
> Hi, I'm struggling here to do the following with any success:

> I have a comma delimited file where each line in the file is something
> like:

> PNumber,3056,Contractor,XYZ Contracting,Architect,ABC Architects,...

This is NOT a CSV file. A CSV file would be :

PNumber,Contractor,Architect,...
2056,XYZ Contracting,ABC Architects,...

Then, you could use the built-in CSV module of recent python versions.

> So each line is intended to be: key1,value1,key2,value2,key3,value3...
> and each line is to be variable in length (although it will have to be
> an even number of records so that each key has a value).

> I want to read in this csv file and parse it into a list of
> dictionaries. So each record in the list is a dictionary:

> {"PNumber":"3056","Contractor":"XYZ Contracting", ... }

> I have no problem reading in the CSV file to a list and splitting each
> line in the file into its comma separated values. But I can't figure
> out how to parse each resulting list into a dictionary.

> Any help on this?

By,

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Jun 7 2005, 4:29 pm
Newsgroups: comp.lang.python
From: John Machin <sjmac...@lexicon.net>
Date: Wed, 08 Jun 2005 06:29:59 +1000
Local: Tues, Jun 7 2005 4:29 pm
Subject: Re: Reading a CSV file into a list of dictionaries

CSV is an acronym for "Comma-Separated Values". It does not imply
anything about the contents of the fields. The OP's file *is* a CSV
file. Yes, the contents do represent an unusual application of the CSV
format -- however a bus full of parcels instead of people is still a bus.

> Then, you could use the built-in CSV module of recent python versions.

Python is a case-sensitive language. The name of the module is "csv".
The OP could use the csv module with his data.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Laurent RAHUEL  
View profile  
 More options Jun 7 2005, 6:02 pm
Newsgroups: comp.lang.python
From: Laurent RAHUEL <laurent.rah...@net-ng.com>
Date: Wed, 08 Jun 2005 00:02:53 +0200
Local: Tues, Jun 7 2005 6:02 pm
Subject: Re: Reading a CSV file into a list of dictionaries

I thought you knew the number of cols and what you should expect in each.
Then it sounded pretty easy to build a list of dictionaries. If you don't
know what you're supposed to find in your file and how this file is
structured I guess you don't know what you are doing.

>> Then, you could use the built-in CSV module of recent python versions.

> Python is a case-sensitive language. The name of the module is "csv".
> The OP could use the csv module with his data.

Damn, that's why I always see those annoynig import errors.

I just wanted to help, maybe you're to much case-sensitive.

Regards,

Laurent.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Kern  
View profile  
 More options Jun 7 2005, 6:31 pm
Newsgroups: comp.lang.python
From: Robert Kern <rk...@ucsd.edu>
Date: Tue, 07 Jun 2005 15:31:43 -0700
Local: Tues, Jun 7 2005 6:31 pm
Subject: Re: Reading a CSV file into a list of dictionaries

Laurent RAHUEL wrote:
> I thought you knew the number of cols and what you should expect in each.
> Then it sounded pretty easy to build a list of dictionaries. If you don't
> know what you're supposed to find in your file and how this file is
> structured I guess you don't know what you are doing.

That's not what the OP asked about.

[RFQ:]
"""So each line is intended to be: key1,value1,key2,value2,key3,value3...
and each line is to be variable in length (although it will have to be
an even number of records so that each key has a value)."""

The rows are not all of the same format. The OP *does* know the
structure, and he (?) *does* know what he's doing. It's just not the
structure usually used in CSV files.

The csv module, of course, still reads these rows just fine; they just
need to be processed a bit to get the correct dictionaries.

--
Robert Kern
rk...@ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google