jeudi 20 juin 2019

Is it possible to have dynamic conditions for an if statement in python?

I have JSON data and I need to loop through it and pass each iteration through an if statement. The if condition though, needs to be changeable. The user enters a condition and that needs to be used in the if statement.

  "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"
    }
  ]
}

I want something like this:

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

So the if should actually be

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

Is this possible?

Aucun commentaire:

Enregistrer un commentaire