Really simple way to deal with XML in Python?

前端 未结 9 1306
Happy的楠姐
Happy的楠姐 2020-12-04 22:36

Musing over a recently asked question, I started to wonder if there is a really simple way to deal with XML documents in Python. A pythonic way, if you will.

<
9条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 22:48

    lxml has been mentioned. You might also check out lxml.objectify for some really simple manipulation.

    >>> from lxml import objectify
    >>> tree = objectify.fromstring(your_xml)
    >>> tree.weather.attrib["module_id"]
    '0'
    >>> tree.weather.forecast_information.city.attrib["data"]
    'Mountain View, CA'
    >>> tree.weather.forecast_information.postal_code.attrib["data"]
    '94043'
    

提交回复
热议问题