python读和写xml文件

python读和写xml文件

读取xml文件

在pascal_voc里面就常见到这种,整理一下。

比如一个原文件是这样的

<annotation>
    <folder>VOC2007</folder>
    <filename>008973.jpg</filename>
    <source>
        <database>The VOC2007 Database</database>
        <annotation>PASCAL VOC2007</annotation>
        <image>flickr</image>
        <flickrid>335707085</flickrid>
    </source>
    <owner>
        <flickrid>kjmurray</flickrid>
        <name>Katherine Murray</name>
    </owner>
    <size>
        <width>500</width>
        <height>333</height>
        <depth>3</depth>
    </size>
    <segmented>1</segmented>
    <object>
        <name>cow</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>271</xmin>
            <ymin>43</ymin>
            <xmax>444</xmax>
            <ymax>279</ymax>
        </bndbox>
    </object>
</annotation>

那读的时候可以这样读

Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse("008973.xml")
>>> objs = tree.findall("object")
>>> objs
[<Element 'object' at 0x7fa96516e9a8>]
>>> imgpath = tree.find("filename").text.strip()
>>> imgpath
'008973.jpg'
>>> for idx, obj in enumerate(objs):
...     bbox = obj.find("bndbox")
...     x1 = float(bbox.find("xmin").text)-1
...     print(x1)
... 
270.0
>>> 

xml 的写入

from xml.dom.minidom import Document

def main():
    # 创建dom文档
    doc = Document()

    # 创建根节点
    orderlist = doc.createElement("orderlist")

    # 根节点insert dom 树
    doc.appendChild(orderlist)

    #
    mydict = dict(zip(range(5), range(0,5,2)))
    for k, v in mydict.items():

        # 每一组信息都要先创建节点order,后insert到<orderlist>下面
        order = doc.createElement("order")
        orderlist.appendChild(order)

        # 下面的也是先创建节点然后insert到其父节点上面。
        customer = doc.createElement("customer")
        customer_text = doc.createTextNode(str(k))
        customer.appendChild(customer_text)
        order.appendChild(customer)

    #with open("xml.xml", "w") as w:
    #    w.write(doc.toprettyxml(indent='\t'))

    with open("xml.xml", "wb+") as w:
        w.write(doc.toprettyxml(indent='\t', encoding="utf-8"))
    return

if __name__ == '__main__':
    main()

写完后的结果是

<?xml version="1.0" encoding="utf-8"?>
<orderlist>
    <order>
        <customer>0</customer>
    </order>
    <order>
        <customer>1</customer>
    </order>
    <order>
        <customer>2</customer>
    </order>
</orderlist>
~                        

打赏,谢谢~~

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,多谢支持~

打开微信扫一扫,即可进行扫码打赏哦