我正在用python处理一些json格式的日志文件。编写一些条件查询非常简单,例如
line=[1,'runtime',{'elapsed':12.3,'jobname':'high38853'}] # read from json
# split the record and see what jobs take over 30 seconds
key,category,details=line
if category == 'runtime' and details['elapsed'] > 30:
print details
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以安全地将字符串解释为条件表达式的一部分,以便我可以在命令行上接受条件并将其作为查询的一部分?
search 'details["elapsed"] > 30'
Run Code Online (Sandbox Code Playgroud)
这样我就可以在代码中做这样的事情?
if *something involving sys.argv[1]*:
print line
Run Code Online (Sandbox Code Playgroud) python ×1