【发布时间】:2017-01-23 01:02:34
【问题描述】:
我有一个 json 文件“OpenEnded_mscoco_val2014.json”。该 json 文件包含 121,512 个问题。
这是一些示例:
"questions": [
{
"question": "What is the table made of?",
"image_id": 350623,
"question_id": 3506232
},
{
"question": "Is the food napping on the table?",
"image_id": 350623,
"question_id": 3506230
},
{
"question": "What has been upcycled to make lights?",
"image_id": 350623,
"question_id": 3506231
},
{
"question": "Is this an Spanish town?",
"image_id": 8647,
"question_id": 86472
}
]
我使用jq -r '.questions | [map(.question), map(.image_id), map(.question_id)] | @csv' OpenEnded_mscoco_val2014_questions.json >> temp.csv 将 json 转换为 csv。
但是这里 csv 中的输出是问题,后跟 image_id,这就是上面代码的作用。
预期的输出是:
"What is table made of",350623,3506232
"Is the food napping on the table?",350623,3506230
是否也可以只过滤具有image_id <= 10000 和group questions having same image_id 的结果?例如json的1,2,3个结果可以组合成3个问题,1个image_id,3个question_id。
编辑:possible duplicate question解决了第一个问题。我想知道是否可以在 jq 的命令行上调用比较运算符来转换 json 文件。在这种情况下,如果仅image_id <= 10000,则从 json 中获取所有字段。
【问题讨论】:
-
不太清楚你的第一个问题是什么?
-
我想使用 jq 过滤 image_id 的值
-
1.请修正问题,以便示例输入是有效的 JSON。 2. 如果问题的重点是 (.questions | length) 太大以至于您不想将整个文件读入内存,那么请说出来。 (在这种情况下,jq 有一个流解析器可能会提供帮助。)
标签: python json csv filtering jq