Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading a CSV file into a list of dictionaries

110 views
Skip to first unread message

RFQ

unread,
Jun 6, 2005, 1:13:58 PM6/6/05
to
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?

Peter Otten

unread,
Jun 6, 2005, 1:34:24 PM6/6/05
to
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

Robert Kern

unread,
Jun 6, 2005, 1:28:00 PM6/6/05
to pytho...@python.org

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

GMane Python

unread,
Jun 7, 2005, 12:34:08 PM6/7/05
to pytho...@python.org
Sounds like you want to use the ConfigObject module.

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

-dave

"RFQ" <rfqu...@gmail.com> wrote in message
news:1118078038....@f14g2000cwb.googlegroups.com...

> --
> http://mail.python.org/mailman/listinfo/python-list
>

Laurent RAHUEL

unread,
Jun 7, 2005, 3:57:24 PM6/7/05
to
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,

John Machin

unread,
Jun 7, 2005, 4:29:59 PM6/7/05
to Laurent RAHUEL
Laurent RAHUEL wrote:
> 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,...
>

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.

Laurent RAHUEL

unread,
Jun 7, 2005, 6:02:53 PM6/7/05
to
John Machin wrote:

> Laurent RAHUEL wrote:
>> 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,...
>>
>
> 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.

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.

Robert Kern

unread,
Jun 7, 2005, 6:31:43 PM6/7/05
to pytho...@python.org
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.

0 new messages