如何使用python纠正XML格式

时间:2019-06-27 02:42:28

标签: python xml

我想通过从<中选择最小值和最大值来更正xml格式。 x1>, ... 以及多边形节点下,... 元素的最小值和最大值,然后将它们映射为正确的格式。 这是格式错误的xml。

<annotation>
    <folder>tmpnm4z0rlj</folder>
    <filename>cjxbkhncg7y7v0884ocby0g4z.png</filename>
    <path>/tmp/tmpnm4z0rlj/cjxbkhncg7y7v0884ocby0g4z.png</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>507</width>
        <height>224</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>2</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <polygon>
            <x1>25</x1>
                <y1>32</y1>
            <x2>25</x2>
                <y2>142</y2>
            <x3>94</x3>
                <y3>142</y3>
            <x4>93</x4>
                <y4>32</y4>
        </polygon>
    </object>
    <object>
        <name>3</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
                <xmin>271</xmin>
                <ymin>21</ymin>
                <xmax>329</xmax>
                <ymax>129</ymax>
        </bndbox>
    </object>
    <object>
        <name>A</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <polygon>
            <x1>170</x1>
                <y1>27</y1>
            <x2>241</x2>
                <y2>141</y2>
            </polygon>
        </object>
    <object>
        <name>BKK</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
                <xmin>156</xmin>
                <ymin>146</ymin>
                <xmax>363</xmax>
                <ymax>216</ymax>
        </bndbox>
    </object>
</annotation>

所以,这是我的预期输出。但是我不知道如何指定那些x和y节点。

<annotation>
    <folder>tmpnm4z0rlj</folder>
    <filename>cjxbkhncg7y7v0884ocby0g4z.png</filename>
    <path>/tmp/tmpnm4z0rlj/cjxbkhncg7y7v0884ocby0g4z.png</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>507</width>
        <height>224</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>2</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
                <xmin>25</xmin>
                <ymin>32</ymin>
                <xmax>94</xmax>
                <ymax>142</ymax>
        </bndbox>
    <object>
        <name>3</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
                <xmin>271</xmin>
                <ymin>21</ymin>
                <xmax>329</xmax>
                <ymax>129</ymax>
            </bndbox>
    </object>
    <object>
        <name>A</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
                <xmin>170</xmin>
                <ymin>27</ymin>
                <xmax>241</xmax>
                <ymax>141</ymax>
        </bndbox>
    </object>
    <object>
        <name>BKK</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
                <xmin>156</xmin>
                <ymin>146</ymin>
                <xmax>363</xmax>
                <ymax>216</ymax>
         </bndbox>
        </object>
</annotation>

0 个答案:

没有答案