因此,我希望能够基于特定条件提取数据,然后希望能够从该查询中打印多个项目...这是我到目前为止所做的事情:
def rec():
qe = JsonQ(r"C:\ShopFloor\data.json")
res = qe.at('data').where('Status', '=', 1).get()
for Process, Shortnum in res:
print(Process['Process'] + " " + Shortnum['Shortnum'])
rec()
这来自以下json文件:
{
"data": [
{
"Shortnum": "34567",
"Process": "SPA",
"Status": 1,
"Start_Time": "2016-12-14 15:54:35",
"Finish_Time": "2016-12-14 15:56:02"
},
{
"Shortnum": "34567",
"Process": "Figure",
"Status": 0,
"Start_Time": "2016-12-08 15:34:05",
"Finish_Time": "2016-12-08 15:34:09"
},
如何使它正常工作?理想情况下,我正在寻找印刷品中的这种响应:
SPA 34567
无法获得当前输出,我收到此错误...我意识到我传递了太多参数,但是我想不出合适的方法...
Exception has occurred: ValueError
too many values to unpack (expected 2)
File "C:\ShopFloor\main.py", line 101, in rec
for Process, Shortnum in res:
File "C:\ShopFloor\main.py", line 106, in <module>
rec()
答案 0 :(得分:3)
在Python中使用JSON的典型方法是将JSON对象作为Python dict
加载:
import json
with open('C:/ShopFloor/data.json', 'r') as f:
qe = json.load(f)
for item in qe['data']:
if item['Status'] == 1:
print(f'{item["Process"]} {item["Shortnum"]}')
请注意,这使用Python 3's f-strings(在访问f字符串中的字典值时,请确保单引号和双引号交替出现)。在Python 2中,将最后一行替换为:
print('{} {}'.format(item['Process'], item['Shortnum']))