关键字作为创建XML文件的参数

时间:2019-05-02 12:23:29

标签: python xml arguments keyword

因此,我想创建一个XML文件,我必须添加的属性之一称为“ from”。当我想补充一点时,我显然会收到“关键字不能是表达式”错误。我必须做些什么才能使它起作用?

routes = ET.Element("routes")
for i,f in df.iterrows():
    flow = ET.SubElement(routes, "flow", id=str(i), from=f["source"], to = f["sink"])

输出应如下所示:

<routes>
  <flow id="0" from="A" to="B"></flow>
  <flow id="1" from="B" to="C"></flow>
  ...
</routes>

2 个答案:

答案 0 :(得分:0)

尝试一下:

# seed.py
User = get_user_model()

print('Creating Admin user...')
admin = User.objects.create_superuser(username='admin',
                                      email='admin@foo.com',
                                      password='foobar')
admin.is_active = True
admin.save()
# To avoid email verifiaction, we add the below line to each user
EmailAddress.objects.create(user=admin, email=admin.email, verified=True)

答案 1 :(得分:0)

ET.SubElement接受attrib kwarg,尤其是在这种情况下:

flow = ET.SubElement(routes, "flow", attrib={'id': str(i),
                                             'from': f["source"],
                                             'to': f["sink"]})