[英]Python extract values from JSON
我正在寻找从JSON中提取值集并将其写入文件的方法。
JSON的格式如下:
"interactions": [
{
"type": "free",
"input": [
[ 1, 4594, 119218, 0, [71, 46], [2295, 1492], [71, 46], [2295, 1492], 16017, 520790446, [71, 46, 71, 46], [71, 46, 71, 46] ],
[ 1, 4594, 119219, 0, [72, 46], [2323, 1492], [72, 46], [2323, 1492], 26016, 520790456, [72, 46, 72, 46], [72, 46, 72, 46] ],
[ 1, 4594, 119220, 0, [72, 45], [2323, 1464], [72, 45], [2323, 1464], 26016, 520790466, [72, 45, 72, 45], [72, 45, 72, 45] ],
[ 1, 4594, 119221, 0, [72, 45], [2323, 1464], [72, 45], [2323, 1464], 26016, 520790476, [72, 45, 72, 45], [72, 45, 72, 45] ],
[ 1, 4594, 119222, 0, [73, 45], [2350, 1464], [73, 45], [2350, 1464], 26016, 520790486, [73, 45, 73, 45], [73, 45, 73, 45] ],
[ 1, 4594, 119223, 0, [73, 45], [2350, 1464], [73, 45], [2350, 1464], 26016, 520790496, [73, 45, 73, 45], [73, 45, 73, 45] ],
[ 1, 4594, 119224, 0, [73, 45], [2350, 1464], [73, 45], [2350, 1464], 46000, 520790506, [73, 45, 73, 45], [73, 45, 73, 45] ]
]
我需要提取的是[71,46]列,然后是以520790446开头的列,并将其写入输出文件。
以下是我目前所获得的代码:
import json
json_data = open("test_json.json")
data = json.load(json_data)
json_data.close()
# Need some sort of nested loop here to iterate through each line of the block, and each block also.
print data["interactions"][0]["input"][0][4], '\t', data["interactions"][0]["input"][0][9]
其中有几个可变长度的块,我需要提取所有值,直到文件结束。 我仍然停留在循环结构中。
有人可以帮忙吗?
您可以像这样获得数据:
[x[4] for x in data["interactions"][0]["input"]]
[x[9] for x in data["interactions"][0]["input"]]
或一口气
[[x[4], x[9]] for x in data["interactions"][0]["input"]]
要回答评论的第一部分:
[[x[4], x[9]] for x in interaction["input"] for interaction in data["interactions"]]
def gen_vals(data):
for i in xrange(len(data["interactions"])):
for j in data["interactions"][i]["input"]:
yield (j[4], j[9])
这是一个可以这样使用的生成器:
vals = [x for x in gen_vals(data)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.