【问题标题】:create attribute xml using python使用python创建属性xml
【发布时间】:2022-10-18 14:50:20
【问题描述】:

如何在 xml 中编辑标签并使用 python 赋予属性?

步骤1 : 更改名称标签(删除空格)--> 标记<Document_Type><Document>

第2步 : 为标签赋予属性 --> <Document><Document Type ="SPTInduk">

下面是一个示例 .xml 及其期望

前:

<?xml version='1.0' encoding='utf-8'?>
<batch>
  <Document_Type>
    <Ekspor>0.0</Ekspor>
    <PPNDipungutSendiriDPP>269789662720400.0</PPNDipungutSendiriDPP>
  </Document_Type>
</batch>

预期成绩:

<?xml version='1.0' encoding='utf-8'?>
<batch>
  <Document Type="SPTInduk">
    <Ekspor>0.0</Ekspor>
    <PPNDipungutSendiriDPP>269789662720400.0</PPNDipungutSendiriDPP>
  </Document>
</batch>

【问题讨论】:

  • 到目前为止,您尝试了什么,有什么问题?
  • 将其更改为字符串,然后用新文本替换旧文本,然后另存为 xml
  • 请分享代码,我们可以看看

标签: python xml attributes tags


【解决方案1】:
stringroot = stringroot.replace('<Document>','<Document Type="SPTInduk">')
stringroot = ET.fromstring(stringroot)
stringroot = ET.ElementTree(stringroot)
stringroot.write(r"{}{}.xml".format(eksport_folder,file_name), encoding="utf-8")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    相关资源
    最近更新 更多