简体   繁体   English

cx_Oracle查询结果到嵌套字典

[英]cx_Oracle query results to nested dictionary

I need to create a set of dictionaries from Oracle query results. 我需要从Oracle查询结果中创建一组字典。

My data is laid out into station ID, date, time, location, species, and catch weight. 我的数据被列入了车站ID,日期,时间,地点,物种和重量。 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, 首先,我需要通过fieldname和value创建一个键控字典,例如stationID,date等,这样当我打电话时,比方说,

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. 最终结果是将所有“喜欢”的电台ID,日期等集成到一个唯一的密钥中 - 结果数据(鱼捕获)是值。

such as [date,time,location, etc] : cod 47, hake 31, dogfish 5 例如[date,time,location, etc] :鳕鱼47,鳕鱼31,鲨鱼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? 然后将其嵌入到解析每个单独样本的“结果”字典中?

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: 只压缩descline ,你不需要使用cursor.description元组中的第二项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM