有没有办法让 python 脚本执行 JSON 查找?

Is there a way to make python script execute JSON lookup?

我试图找到它可能存在的位置,但我不知道要搜索的正确词。我有一个简单的脚本,我不得不将查找函数硬编码到 return 每个后续值,但我需要以某种方式自动化它,以便我可以简单地添加一个值,例如 'quantity' 并且它会响应相应的值。 我使用的 JSON 文件是

[{"_id": {
"$oid": "5968dd23fc13ae04d9000001"},
"product_name": "sildenafil citrate",
"supplier": "Wisozk Inc", "quantity": 261,
"unit_cost": ".47"}, 
{"_id": {"$oid": "5968dd23fc13ae04d9000002" }, 
"product_name": "Mountain Juniperus ashei",
"supplier": "Keebler-Hilpert",
"quantity": 292,
"unit_cost": ".74"}, 
{ "_id": {
"$oid": "5968dd23fc13ae04d9000003" },
"product_name": "Dextromathorphan HBr",
"supplier": "Schmitt-Weissnat",
"quantity": 211,
"unit_cost": ".53"}]

我在 Spyder 中编写的当前 Python 2.7 代码(使用 stackoverview 帮助)允许我 return 所需的值是:

import json
products = open('C:\Users\*location on my computer*\products.json')
readable_json = json.load(products)
for i in readable_json:
print i['_id'], i['product_name'], i['supplier'], i['quantity'], i['unit_cost']

最后,我希望能够 运行 我的脚本通过类似于 "python jsonextractor.py ./products.json product_name, unit_cost" 的方式生成 product_name 和 unit_cost 的值。非常感谢任何帮助或让我知道去哪里找。

您基本上需要学习如何阅读命令行参数。

你可以参考这里的答案来了解一下:How to read/process command line arguments?