python3怎么读写XML

python3怎么读写XML

<annotation>

<folder>X</folder>

<filename>P00278.jpg</filename>

<path>/home/hello/X/P00278.jpg</path>

<source>

<database>Unknown</database>

</source>

<size>

<width>825</width>

<height>542</height>

<depth>3</depth>

</size>

<segmented>0</segmented>

<object>

<name>knife</name>

<pose>Unspecified</pose>

<truncated>0</truncated>

<difficult>0</difficult>

<bndbox>

<xmin>100</xmin>

<ymin>287</ymin>

<xmax>497</xmax>

<ymax>457</ymax>

</bndbox>

</object>

</annotation>

读取XML
返回 bndbox 里面4个坐标

写XML
修改 bndbox 里面4个坐标


回答:

from lxml import etree

#读取

with open("file.xml") as fp:

doc=etree.XML(fp.read())

n=doc.xpath("//banbox/xmin")

print(int(n.text)) #其他的类似

#改写

n.text=str(123)

with open("output.xml","w") as fp:

fp.write(etree.dump(doc))

以上是 python3怎么读写XML 的全部内容, 来源链接: utcz.com/p/937941.html

回到顶部