我试图用Python动态创建一个函数并执行它。到目前为止,我得到的是:
def ExecuteScript(saveFile, updatedSaveFile, codeSnippet ):
decryptedData, jsonData = FixSave.GetDataFromSaveFile(saveFile)
FixSave.Save(decryptedData, jsonData, updatedSaveFile + "_original")
dynamicFunction = ""
dynamicFunction += "def execCodeSippet(jsonData):\n"
for line in codeSnippet.splitlines():
line = " " + line.strip() + "\n"
dynamicFunction += line
dynamicFunction += " return jsonData\n"
#execCodeSnippetD = {}
exec(dynamicFunction) # in execCodeSnippetD
#print(execCodeSnippetD)
#exec("print(execCodeSnippetD)")
jsonData = execCodeSnippet(jsonData)
FixSave.Save(decryptedData, jsonData, updatedSaveFile)
我读过exec应该在当前名称空间中创建函数,但是它没有。接下来我需要做什么? 我试图在字典中执行它,但它返回一个空的。在
其思想是让用户定义在Json文件中修改哪些值。在
编辑: 我也试过了
^{pr2}$但我仍然得到:“module”对象没有属性“execcodesippet”
下面是我如何做到的(Python3):
相关问题 更多 >
编程相关推荐