【发布时间】:2020-04-19 06:59:15
【问题描述】:
我正在尝试打印一个字符串,在一行中调用函数和变量。
如【你好! %(job), %(name)s, (function_name)]->[Hello! student, John, This is the function.]
json01.json
{
"test" : "Hello! %(job), %(name)s, (function_name)"
}
test01.py
import json
a = 'test'
name = 'John'
job = 'student'
def function_name(message):
print(message)
with open('json01.json') as json_file:
json_dict = json.load(json_file)
if a in json_dict:
print(json_dict[a] %locals())
#This works if there's only variables in the value
#but I don't know how to call functions when the value is not only function's name but also variables..
是否有任何简单的方法来打印它们的值? 还是有其他方法可以完成这项工作?
抱歉解释不佳,谢谢!
【问题讨论】:
-
尝试使用
eval() -
哦,是的,我使用了 eval() 并且它有效,但我想在不使用 eval() 的情况下执行此操作.. 有什么想法吗?
-
嗯,从字符串中调用变量,我只能拿出
eval()函数。 -
hmm.. 如果没有
eval()..,可能无法从字符串中同时调用变量和函数? -
你想向函数传递参数吗?
标签: python json python-3.x dictionary