Reading a CSV file into a list of dictionaries

Robert Kern rkern at ucsd.edu
Tue Jun 7 18:31:43 EDT 2005


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
rkern at ucsd.edu

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




More information about the Python-list mailing list