python中的if语句是否可以具有动态条件?

时间:2019-06-20 12:53:52

标签: python json if-statement

我有JSON数据,我需要遍历它,并通过if语句传递每次迭代。但是,如果条件,则需要更改。用户输入一个条件,并且需要在if语句中使用该条件。

  "Report": [
    {
      "Column Name": "Name",
      "Value": "Some Name",
      "Some key": "Some Value",
      "Some other key": "Some other Value",
    },
    {
      "Column Name": "Age",
      "Value": 62,
      "Some key": "Some Value",
      "Some other key": "Some other Value",
    },
    {
      "Column Name": "Year",
      "Value": 1957,
      "Some key": "Some Value",
      "Some other key": "Some other Value",
    },
    {
      "Column Name": "Month",
      "Value": "May",
      "Some key": "Some Value",
      "Some other key": "Some other Value",
    },
    {
      "Column Name": "Date",
      "Value": 26,
      "Some key": "Some Value",
      "Some other key": "Some other Value"
    }
  ]
}

我想要这样的东西:

condition = sys.stdin.readline() 
#something like this --> d['Report'][i]['Column Name'] == "Age" and d['Report'][i]['Value'] > 50

for i in range(len(d['Report'])):
    if condition <-- user defined
       print something

因此,如果实际上应该是

if d['Report'][i]['Column Name'] == "Age" and d['Report'][i]['Value'] > 50:
   print something

这可能吗?

0 个答案:

没有答案