简体   繁体   English

如何使用 Python 和 append 某些字段遍历 XML 文件列表?

[英]How can I iterate through a list of XML files using Python and append certain fields?

Essentially I have a series of XML files in PASCALVOC format but the annotations are wrong, and are off by a factor of 10. I need to iterate through the files and essentially add a '0' to specific fields (xmax, xmin, ymax, etc.).本质上,我有一系列 PASCALVOC 格式的 XML 文件,但注释是错误的,并且相差 10 倍。我需要遍历文件并基本上将“0”添加到特定字段(xmax、xmin、ymax、 ETC。)。 The XML files all look like this: XML 文件全部如下所示:

<folder>VOC2014</folder>
<filename>2014_000001.png</filename>
<source>
    <database>PASCAL VOC Compatible Annotation Database</database>
    <annotation>Department of Electrical Engineering</annotation>
    <image>PASCAL</image>
</source>
<segmented>0</segmented>
<object>
    <name>car</name>
    <bndbox>
        <xmax>592</xmax>
        <xmin>183</xmin>
        <ymax>338</ymax>
        <ymin>1</ymin>
    </bndbox>
    <difficult>0</difficult>
    <occluded>1</occluded>
    <pose>Frontal</pose>
    <truncated>0</truncated>
</object>
<size>
    <depth>1</depth>
    <height>400</height>
    <width>600</width>
</size>

Whereas in this scenario I want xmax to be appended to 5920, xmin to be appended to 1830. The ElementTree module seems promising but I'm having trouble with the Find and Replace functions across multiple files.而在这种情况下,我希望将 xmax 附加到 5920,将 xmin 附加到 1830。ElementTree 模块似乎很有希望,但我在跨多个文件的查找和替换功能时遇到了问题。 Any help would be greatly appreciated, thanks!任何帮助将不胜感激,谢谢!

Your sample xml is not well formed (it needs to wrapped in a root element), but assuming that is fixed you can try something like this:您的示例 xml 格式不正确(它需要包装在根元素中),但假设已修复,您可以尝试以下操作:

import xml.etree.ElementTree as ET

bnd = """your xml above, fixed"""

doc = ET.fromstring(dnd)
for d in doc.findall('.//object/bndbox'):
    for line in d.findall('*'):
        line.text= str(int(line.text)*10)
print(ET.tostring(doc).decode())

The output should have all <bndbox> child nodes with a value equal to 10 times the original. output 的所有<bndbox>子节点的值应等于原始值的 10 倍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM