Editing values in a xml file with Python

后端 未结 5 1331
执笔经年
执笔经年 2021-01-17 03:08

Hey. I want to have a config.xml file for settings in a Python web app.

I made car.xml manually. It looks like this:


    

        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-17 03:25

    Without addressing the merits of using XML instead of a Python module for managing configuration files, here's how to do what you asked using lxml:

    >>> from lxml import etree
    >>> xml = """
       
          on
       
    """
    >>> doc = etree.fromstring(xml)
    >>> elm = doc.xpath("/car/lights/blinkers")[0]
    >>> elm.text="off"
    >>> etree.tostring(doc)
    '\n   \n      off\n   \n'
    

提交回复
热议问题