函数中的 exec 函数不起作用(python 3.5) [英] exec function in a function is not working(python 3.5)
问题描述
我正在尝试在 python 中编写一个动态程序(根据它们的标签解析 json 文件),我正在使用 Python exec 函数来做到这一点.但是当 exec 语句在函数中时,程序会失败.
I am trying to write a dynamic program(to parse json files based on their tags) in python and i am using Python exec function to do that. But the program is failing when exec statement is in a function.
运行 1:在函数中执行:
RUN 1: Exec in a function:
import json
from pandas.io.json import json_normalize
import sys
def param(dfile):
aString = "['widget']['image']~['widget']['window']~['widget']['text']"
for nd_part in aString.split('~'):
exec("temp = %s%s"%(dfile,nd_part))
print(temp)
if __name__ == "__main__":
dfile = json.load(open("sample_json.json"))
str_list = param(dfile)
JSON 数据:sample_json.json
JSON Data: sample_json.json
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
错误:
Traceback (most recent call last):
File "sample_test_json.py", line 12, in <module>
str_list = param(dfile)
File "sample_test_json.py", line 9, in param
print(temp)
NameError: name 'temp' is not defined
运行 2:主要执行:
import json
from pandas.io.json import json_normalize
import sys
if __name__ == "__main__":
dfile = json.load(open("sample_json.json"))
aString = "['widget']['image']~['widget']['window']~['widget']['text']"
for nd_part in aString.split('~'):
exec("temp = %s%s"%(dfile,nd_part))
print(temp)
JSON 数据:sample_json.json(数据同上)
JSON Data: sample_json.json(same data as above)
输出:没有错误(结果如预期)
Output: No error(Result as expected)
{'hOffset': 250, 'vOffset': 250, 'name': 'sun1', 'alignment': 'center', 'src': 'Images/Sun.png'}
{'width': 500, 'height': 500, 'name': 'main_window', 'title': 'Sample Konfabulator Widget'}
{'data': 'Click Here', 'hOffset': 250, 'vOffset': 100, 'size': 36, 'style': 'bold', 'onMouseUp': 'sun1.opacity = (sun1.opacity / 100) * 90;', 'name': 'text1', 'alignment': 'center'}
运行 3:我尝试了 eval 并尝试格式化这篇文章中的字符串.如何从函数中的 exec 返回值?
RUN 3: I tried eval and tried formatting the string from this post. How to return value from exec in function?
import json
from pandas.io.json import json_normalize
import sys
def param(dfile):
aString = "['widget']['image']~['widget']['window']~['widget']['text']"
for nd_part in aString.split('~'):
exec('temp = "{}""{}"'.format(dfile,nd_part))
print(temp)
if __name__ == "__main__":
dfile = json.load(open("sample_json.json"))
str_list = param(dfile)
错误:
Traceback (most recent call last):
File "sample_test_json.py", line 12, in <module>
str_list = param(dfile)
File "sample_test_json.py", line 9, in param
print(temp)
NameError: name 'temp' is not defined
请帮助我找出问题所在.提前致谢.
Please help me in identifying the issue. Thanks in advance.
推荐答案
使用 Eval() 我得到了我期望的结果.在下面发布答案.但我仍然不确定为什么 exec() 不起作用.
Using Eval() i am getting the result i exepcted. Posting answer below. But still i am not sure why exec() is not working.
import json
from pandas.io.json import json_normalize
import sys
def param(dfile):
aString = "['widget']['image']~['widget']['window']~['widget']['text']"
for nd_part in aString.split('~'):
s = '{0}{1}'.format('dfile',nd_part)
temp = eval(s)
print(temp)
if __name__ == "__main__":
dfile = json.load(open("sample_json.json"))
str_list = param(dfile)
结果:
{'src': 'Images/Sun.png', 'alignment': 'center', 'vOffset': 250, 'name': 'sun1', 'hOffset': 250}
{'title': 'Sample Konfabulator Widget', 'height': 500, 'width': 500, 'name': 'main_window'}
{'alignment': 'center', 'onMouseUp': 'sun1.opacity = (sun1.opacity / 100) * 90;', 'data': 'Click Here', 'hOffset': 250, 'size': 36, 'vOffset': 100, 'name': 'text1', 'style': 'bold'}
这篇关于函数中的 exec 函数不起作用(python 3.5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!