简体   繁体   English

使用Python编辑XML文件内容

[英]Editing XML file content with Python

I am trying to use Python to read in an XML file containing some parameter names and values, eg 我正在尝试使用Python来读取包含一些参数名称和值的XML文件,例如

   ...
   <parameter name='par1'>
        <value>24</value>
   </parameter>
   <parameter name='par2'>
        <value>Blue/Red/Green</value>
   </parameter>
   ...

and then pass it a dictionary with the parameter names {'par1':'53','par2':'Yellow/Pink/Black',...} and corresponding new values to replace the old ones in the XML file. 然后传递一个字典,参数名称为''par1':'53','par2':'Yellow / Pink / Black',...}和相应的值,以替换XML文件中的旧值。 The output should then overwrite the original XML file. 然后输出应覆盖原始XML文件。

At the moment I am converting the XML to a python dictionary and after some element comparison and regular expression handling, writing the output again in XML format. 目前我正在将XML转换为python字典,并在进行了一些元素比较和正则表达式处理之后,再次以XML格式编写输出。

I am not too happy with this and was wondering whether anyone can recommend a more efficient way of doing it? 我对此并不满意,并且想知道是否有人可以推荐一种更有效的方法吗?

Thanks. 谢谢。

My first suggestion is to use lxml or some other Python XML parser rather than using regular expressions. 我的第一个建议是使用lxml或其他Python XML解析器而不是使用正则表达式。 XML is not a language that can be parsed with regular expressions reliably. XML不是可以可靠地使用正则表达式解析的语言。 (If you consistently try to parse XML with regular expressions bad things happen ) (如果你一直试图用正则表达式解析XML 会发生坏事

xml.etree.ElementTree is much more pythonic than other XML parsers. xml.etree.ElementTree比其他XML解析器更加xml.etree.ElementTree

An example building a dict based on your data: 根据您的数据构建dict的示例:

>>> src = """<params>
    <parameter name='par1'> <value>24</value> </parameter>
    <parameter name='par2'> <value>Blue/Red/Green</value> </parameter>
    </params>
    """
>>> tree = ElementTree.XML(src)
>>> dict(((i.attrib['name'], i.find('value').text) for i in tree.getiterator('parameter')))
{'par2': 'Blue/Red/Green', 'par1': '24'}
>>> 

After changing dict values, build a new ElementTree using similar methods, and generate an XML file with the write method. 更改dict值后,使用类似方法构建新的ElementTree ,并使用write方法生成XML文件。 Note examples in the referenced doc that show how to modify content directly in the etree structure. 请注意参考文档中的示例,其中显示了如何直接在etree结构中修改内容。 Maybe a dict is not needed. 也许不需要dict

If you have read in both your old and new XML files and stored them as dictionaries, then overwriting the old values is easy - use update() : 如果您已读入旧XML文件和新XML文件并将其存储为字典,则可以轻松覆盖旧值 - 使用update()

dict_old.update(dict_new)

This updates the original dictionary with the key/value pairs from dict_new , overwriting existing keys. 这将使用dict_new的键/值对更新原始字典,覆盖现有键。 Regular expressions are unnecessary, since you presumably want to only match exact keys. 正则表达式是不必要的,因为您可能只想匹配精确的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM