这是以下代码中“数据”的一部分:
{'element': 'background', 'x': 0, 'y': 0, 'width': 800, 'height': 898, 'properties': {'background-color': '#ffffff'}}
编码:
for i in data:
print(i)
if i['element'] in comp:
ta = i['element']
if i['properties']:
prop_keys = list(i['properties'].keys())
background = "green"
for j in range(len(prop_keys)):
attri = prop_keys[j]
print(type(attri))
val = i['properties'][prop_keys[j]]
print(type(val))
doc, tag, text = Doc().tagtext()
exec('with tag(ta, %s = %s)'%(attri,val))
break
我得到的错误:
Traceback (most recent call last):
File "/home/harsh/.virtualenvs/py3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-78-69185a93c0dd>", line 19, in <module>
exec('with tag(ta, %s = %s)'%(attri,val))
File "<string>", line 1
with tag(ta, background-color = #ffffff)
^
SyntaxError: unexpected EOF while parsing
我基本上想使用 'attri' 的值作为 yattag 中的属性名称以使其动态化。我尝试使用 'exec' 函数输入值以使用 yattag 将 json 数据转换为 html,但 yattag 库不允许使用表达式代替变量名来使其动态化。