[英]Take 2 key values from list of python dicts & make new list/tuple/array/dictionary with each index containing 2 key values from 1st listed dict
I have a list of dictionaries in a json file. 我在json文件中有一个字典列表。
I have iterated through the list and each dictionary to obtain two specific key:value pairs from each dictionary for each element. 我已遍历列表和每个字典以获取每个元素的每个字典中的两个特定键:值对。 ie
List[dictionary{i(key_x:value_x, key_y:value_y)}]
即
List[dictionary{i(key_x:value_x, key_y:value_y)}]
My question is now: 我现在的问题是:
How do I place these two new key: value pairs in a new list/dictionary/array/tuple, representing the two key: value pairs extracted for each listed element in the original? 如何将这两个新的键:值对放在新的列表/字典/数组/元组中,表示为原始中每个列出的元素提取的两个键:值对?
To be clear: 要明确:
ORIGINAL_LIST (i.e. with each element being a nested dictionary) =
[{"a":{"blah":"blah",
"key_1":value_a1,
"key_2":value_a2,
"key_3":value_a3,
"key_4":value_a4,
"key_5":value_a5,},
"b":"something_a"},
{"a":{"blah":"blah",
"key_1":value_b1,
"key_2":value_b2,
"key_3":value_b3,
"key_4":value_b4,
"key_5":value_b5,},
"b":"something_b"}]
So my code so far is: 所以到目前为止我的代码是:
import json
from collections import *
from pprint import pprint
json_file = "/some/path/to/json/file"
with open(json_file) as json_data:
data = json.load(json_data)
json_data.close()
for i in data:
event = dict(i)
event_key_b = event.get('b')
event_key_2 = event.get('key_2')
print(event_key_b)#print value of "b" for each nested dict for 'i'
print(event_key_2)#print value of "key_2" for each nested dict for 'i'
To be clear: 要明确:
FINAL_LIST(i.e. with each element being a nested dictionary) =
[{"b":"something_a", "key_2":value_2},
{"b":"something_b", "key_2":value_2}]
So I have an answer to getting the keys into individual dictionaries, as follows in the code below. 所以我有一个答案,将密钥放入单独的字典中,如下面的代码所示。 The only problem is that the value for 'key_2' in the original json dictionaries is either an int value or it is "" for values which are 0. My script just returns 'None' for all instances of value_2 for key_2.
唯一的问题是原始json字典中'key_2'的值是int值,或者对于0的值是“”。对于key_2的所有value_2实例,我的脚本只返回'None'。 How can I get it to read the appropriate values for 'value_2'?
如何让它读取'value_2'的相应值? I want to only return dictionaries for cases where 'value_2' > 0 (ie where value_2 != "")
我想只返回'value_2'> 0(即value_2!=“”)的情况下的词典
Below is the current code: 以下是当前代码:
import json
from pprint import pprint
json_file = "/some/path/to/json/file"
with open(json_file) as json_data:
data = json.load(json_data)
json_data.close()
for i in data:
event_key_b = event.get('b')
for x in i:
event_key_2 = event.get('key_2')
x = {'b' : something_b, 'key_2' : value_2}
print(x)
Also, if there are any more elegant solutions anyone can think of I would really be interested in learning them ... Some of the json files I'm looking at can range from 200 dictionary entries in the original list to 2,000,000. 此外,如果有任何更优雅的解决方案,任何人都可以想到我真的有兴趣学习它们...我正在看的一些json文件可以从原始列表中的200个字典条目到2,000,000个。 I'm planning to feed my parsed results into a message queue for processing by a different service and any efficiencies in the code will help for scalability in processing.
我打算将解析后的结果提供给消息队列,以便由不同的服务进行处理,代码中的任何效率都有助于提高处理的可扩展性。 Also if anyone has any recommendations to give on Redis vs. RabbitMQ, I'd really appreciate it
如果有人对Redis vs. RabbitMQ有任何建议,我真的很感激
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.