By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

Reading a CSV file into a list of dictionaries

P: n/a
RFQ
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?

Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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,...

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.


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

Jul 19 '05 #2

P: n/a
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

Jul 19 '05 #3

P: n/a
Sounds like you want to use the ConfigObject module.

http://www.voidspace.org.uk/python/m...html#configobj

-dave

"RFQ" <rf******@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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?

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


Jul 19 '05 #4

P: n/a
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,

Jul 19 '05 #5

P: n/a
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.
Jul 19 '05 #6

P: n/a
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.

Jul 19 '05 #7

P: n/a
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

Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.