I am working on a program which evaluates a query for a database. Below I define a single record, and show the issue I get when using eval()
record = {'name': 'John_Smith', 'gender': 'M', 'age':45,'Dept.': 'Sales'}
query = "Dept. == 'Sales'"
#query = "gender == 'M'"
if eval(query, {__builtins__: None}, record):
print(record)
The error I get is
File "<string>", line 1
Dept. == 'Sales'
^
SyntaxError: invalid syntax
if query = "gender == 'M'" (as seen in the comment), eval() does work. However, when query is "Dept. == 'Sales'", I get the syntax error. It is clear that the period is causing this problem (I tried with other keys from the dictionary and had no issues). Is there a reason it won't accept the period? Is there a way I can have eval() recognize the period?
Looking forward to your answers!
Thanks!