I'm working on a project that allows users to define filters to sort data in a csv file. They can create as many or as few filters as they want so I am trying to create an if statement from a string that will get longer as users define more filters.
I have an output for my string variable which is this: row[0]=="6/1/2014 0:00:00"
So for simplicity sake I'll just hard code that into a variable name in the sample code below
The problem is that it doesn't correctly evaluate this if statement. It returns every row of the file.
f = open("somefile.csv", "r")
r = csv.reader(f)
statement = 'row[0]=="6/1/2014 0:00:00"'
for row in r:
if statment:
print(row)
Truefor every iteration, since you are checking against a value that is notFalse...eval, but if you don't have 100% trust in your users, thenevalis not safe and you'll need to parse the string yourself (e.g. viaast).eval. might do you some good to create some sort of api for your users that will create filters in a safer manner