Reading a CSV file into a list of dictionaries

R

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?
 
R

Robert Kern

RFQ said:
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
(e-mail address removed)

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

Peter Otten

RFQ said:
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:
{'Architect': 'ABC', 'PNumber': '3056', 'Contractor': 'XYZ Contracting'}

Peter
 
L

Laurent RAHUEL

RFQ said:
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,
 
J

John Machin

Laurent said:
RFQ wrote:




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.
 
L

Laurent RAHUEL

John said:
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.
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.
 
R

Robert Kern

Laurent said:
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
(e-mail address removed)

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

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top