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