开发者

Editing XML file content with Python

开发者 https://www.devze.com 2023-01-01 14:31 出处:网络
I am trying to use Python to read in an XML file containing some parameter names and values, e.g. ... <parameter name=\'par1\'>

I am trying to use Python to read in an XML file containing some parameter names and values, e.g.

   ...
   <parameter name='par1'>
        <value>24</value>
   &l开发者_运维问答t;/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. The output should then overwrite the original XML file.

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.

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. XML is not a language that can be parsed with regular expressions reliably. (If you consistently try to parse XML with regular expressions bad things happen)


xml.etree.ElementTree is much more pythonic than other XML parsers.

An example building a dict based on your data:

>>> 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. Note examples in the referenced doc that show how to modify content directly in the etree structure. Maybe a dict is not needed.


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():

dict_old.update(dict_new)

This updates the original dictionary with the key/value pairs from dict_new, overwriting existing keys. Regular expressions are unnecessary, since you presumably want to only match exact keys.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号