使用python从xml文件中提取数据时出错

时间:2018-06-07 07:59:53

标签: python xml

我正在尝试从xml文件中提取数据并尝试打印对象名称,但我收到此错误:

File "new.py", line 11, in <module>
    dom=ElementTree.parse(image_file)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
  File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 647, in parse
    source = open(source, "rb")
IOError: [Errno 2] No such file or directory: 'abc.xml'

虽然程序知道文件名但仍然没有文件名。这是我的python程序:

import os
from xml.etree import ElementTree
path="/home/sultan/Desktop/xmltxt/xmlfiles"
dirs = os.listdir( path )
savedir='textfiles'
for image_file in (dirs):
    if not os.path.isdir(savedir):
        os.mkdir(savedir)


    dom=ElementTree.parse(image_file)
    labels=dom.findall('object/name')
    for lbl in labels:
       print(lbl)

这是我的xml文件的内容:

<annotation>
<folder>images</folder>
<filename>3rdtrk6.jpg</filename>
<segmented>0</segmented>
<size>
<width>1920</width>
<height>1080</height>
<depth>3</depth>
</size>
<object>
<name>truck</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>959</xmin>
<ymin>388</ymin>
<xmax>1411</xmax>
<ymax>889</ymax>
</bndbox>
</object>
<object>
<name>bus</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox><xmin>220</xmin>
<ymin>264</ymin>
<xmax>471</xmax>
<ymax>465</ymax>
</bndbox>
</object>
</annotation>

1 个答案:

答案 0 :(得分:0)

您需要提供文件的完整路径。使用os.path.join(path, image_file)

<强>实施例

import os
from xml.etree import ElementTree
path="/home/sultan/Desktop/xmltxt/xmlfiles"
dirs = os.listdir( path )
savedir='textfiles'
for image_file in (dirs):
    if not os.path.isdir(savedir):
        os.mkdir(savedir)


    dom=ElementTree.parse(os.path.join(path, image_file))
    labels=dom.findall('object/name')
    for lbl in labels:
       print(lbl)