python - 阅读,修改 xml 使用 python 将其保存在新的 xml 中

标签 python xml xmltodict dicttoxml

以下是我正在执行的步骤:

  1. 以字典形式读取xml文件

    import xmltodict
    
    with open("example.xml") as sxml:
        data = xmltodict.parse(sxml.read())
    
  2. 改变值(value)

    data["key"]["key1"] = "some value"
    
  3. 我想将更改保存在 example.xml 文件中,或者我想创建一个新文件并保存更改。

我该怎么做?

最佳答案

正在关注 README我们可以简单地做

with open('example.xml', 'w') as result_file:
    result_file.write(xmltodict.unparse(data))

如果你想覆盖example.xml 或者

with open('result.xml', 'w') as result_file:
    result_file.write(xmltodict.unparse(data))

如果你想创建新文件result.xml

关于python - 阅读,修改 xml 使用 python 将其保存在新的 xml 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52499959/

相关文章:

python - 日期时间解析错误,格式错误?

javascript - 一次在odoo中应用多个过滤器

javascript - 设置子节点的属性

java - 如何使用 RELAX NG 模式和 JAXP 验证 XML 文档?

python - xmltodict unparse 解析不一样

python - Flask Api 解析返回错误的 xml post 请求

python - 不使用 pandas 的多列标签编码

Python Pandas : Counting keys and summing up their values in a data frame

python - PyAudio 找不到任何输出设备