解析XML未绑定前缀:错误

时间:2016-12-08 04:11:47

标签: python xml

我正在尝试解析输入,但我一直在获取和未绑定的前缀:错误。

我知道这个错误与数据有关:bbox'标记并将其更改为仅数据'解决了这个问题。但我有这么多文件使用相同的标签'数据:bbox'。

有办法吗?

我使用的代码是:

    import xml.etree.ElementTree as ET
    input = '''<data>
    <sourcefile filename ="file:/C:/temp/S1-T1-C/S1-T1-C/video/pets2006/S1-T1-C/3/3.info">

    <object framespan="244:309" id="0" name="Bag objects">
        <attribute name="bagBoundingBox">
            <data:bbox framespan="244:244" height="51" width="47" x="3" y="428"/>
        </attribute>
    </object>

    <object framespan="441:484" id="1" name="Bag objects">
        <attribute name="bagBoundingBox">
            <data:bbox framespan="259:259" height="54" width="72" x="67" y="450"/>
        </attribute>
    </object>

   </sourcefile>
</data>'''

data = ET.fromstring(input)
lst = data.findall('sourcefile/object')
print ('Object count:', len(lst))

for object in lst:
     print ("*****Object*****" + '\n')
     print ('Object ID', object.get("id"))
     print ('Frame numbers:', object.get("framespan"))

boundingBoxes = object.findall('attribute/data')
for boundingBox in boundingBoxes:
    print ("--------Bounding Box --------")     
    print ('Bounding box frame number: ', boundingBox.get("framespan"))
    print ('Height of bounding box: ', boundingBox.get("height"))
    print ('Width of bounding box: ', boundingBox.get("width"))
    print ('X coordinate of bounding box: ', boundingBox.get("x"))
    print ('Y coordinate of bounding box: ', boundingBox.get("y"))

0 个答案:

没有答案