html - 如何在使用 yattag 时动态创建属性?
问题描述
这是以下代码中“数据”的一部分:
{'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 库不允许使用表达式代替变量名来使其动态化。
解决方案
推荐阅读
- php - 在 WooCommerce 中将自定义订单状态设置为已付款
- python - 重启服务器后重启 Python 代码
- azure - 忽略空文件夹 Azure 数据工厂
- c# - 如何在 1 个场景中创建 2 个虚拟按钮
- reactjs - MUI 5 - 使用样式化 API 的关键帧动画不起作用
- python-3.x - 如何将纪元时间转换为字符串?
- javascript - 从 angular 11 升级到 12 后,通过 mixins 对组件进行主题化似乎不再起作用
- android - Facebook Audience Network 上的 Admob 中介公开竞价,广告未加载
- android - Android Studio:发布 apk 中缺少 XX.RSA、XX.SF 文件
- c++ - 如何在 Windows 上无错误地 npm i ssh2?