Question

I need to create a set of dictionaries from Oracle query results.

My data is laid out into station ID, date, time, location, species, and catch weight. For each species, there is a new line, while all the location, etc, data may be redundant.

First, I need to create a keyed dictionary by fieldname and value, such as stationID, date, etc so that when I call, say,

value[1]  (this being the station ID field)

I get

112

The end result is to conglomerate all of the "like" station ID's, dates, etc into one unique key-- with the resulting data (fish catch) being the values.

such as [date,time,location, etc]: cod 47, hake 31, dogfish 5

So far, to get the data parsed into individual keyed dictionaries by field name and then by line I have this:

desc=[d[0] for d in cursor.description]
field=[d[1] for d in cursor.description]
value=dict(zip(desc,field))   
result=[dict(zip(value,line)) for line in cursor]
print result[1]

However, if I try and call value, the field is simply the data type.. how can I get the actual data value? And then nest that into the "result" dictionary that parses each individual sample?

Was it helpful?

Solution

You are zipping together the wrong items. Zip only desc and line, you do not need to use the second item in the cursor.description tuples:

desc=[d[0] for d in cursor.description]
result=[dict(zip(desc, line)) for line in cursor]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top