如何将代码直接应用到a中的所有文件并将xml个文件转换为txt文件
How to apply code to all the files in a directly and convert xml files to txt files
我正在尝试应用 python 中的代码从每个 xml 文件中提取必要的信息并将它们写入 txt 文件。
我成功执行了我的代码,但结果只显示一个文件。
另外,我的代码看起来不太聪明,如果有人能帮我解决问题并给我一些纠正,我会很高兴。
恐怕我是初学者,所以我可能会犯很多基本错误...
这是我的代码
from xml.etree import ElementTree as ET
import os
path = r'/Users/mo/Documents/annotations/xmls'
filenames = []
for filename in os.listdir(path):
if not filename.endswith('.xml'):
continue
fullname = os.path.join(path,filename)
filenames.append(fullname)
each_name = filename[:-4]
with open(each_name + '.txt', 'a') as f:
for filename in filenames:
tree = ET.parse(filename)
root = tree.getroot()
for object in root.findall('object'):
categoryID = object.find('name').text
for bnd_box in object.findall('bndbox'):
Xcenter = (int(bnd_box.find('xmax').text) - int(bnd_box.find('xmin').text))/2
Ycenter = (int(bnd_box.find('ymax').text) - int(bnd_box.find('ymin').text))/2
width = int(bnd_box.find('xmax').text)- int(bnd_box.find('xmin').text)
height = int(bnd_box.find('ymax').text) - int(bnd_box.find('ymin').text)
Detection_rows = str(categoryID) + str(Xcenter) + str(Ycenter) + str(width) + str(height) + '\n'
f.write(str(Detection_rows))
非常感谢
试试这个(没测试)
from xml.etree import ElementTree as ET
import os
path = r'/Users/mo/Documents/annotations/xmls'
for filename in os.listdir(path):
if not filename.endswith('.xml'):
continue
fullname = os.path.join(path, filename)
with open(fullname[:-4] + '.txt', 'a') as f:
tree = ET.parse(fullname)
root = tree.getroot()
for object in root.findall('object'):
categoryID = object.find('name').text
for bnd_box in object.findall('bndbox'):
Xcenter = (int(bnd_box.find('xmax').text) - int(bnd_box.find('xmin').text))/2
Ycenter = (int(bnd_box.find('ymax').text) - int(bnd_box.find('ymin').text))/2
width = int(bnd_box.find('xmax').text)- int(bnd_box.find('xmin').text)
height = int(bnd_box.find('ymax').text) - int(bnd_box.find('ymin').text)
Detection_rows = str(categoryID) + str(Xcenter) + str(Ycenter) + str(width) + str(height) + '\n'
f.write(str(Detection_rows))
您的 for 循环应该在 open
之外并且您将 filenames
更改为 filename
。另外,我删除了一个不必要的 for 循环。如果它能解决您的问题,请告诉我。
我正在尝试应用 python 中的代码从每个 xml 文件中提取必要的信息并将它们写入 txt 文件。 我成功执行了我的代码,但结果只显示一个文件。 另外,我的代码看起来不太聪明,如果有人能帮我解决问题并给我一些纠正,我会很高兴。
恐怕我是初学者,所以我可能会犯很多基本错误...
这是我的代码
from xml.etree import ElementTree as ET
import os
path = r'/Users/mo/Documents/annotations/xmls'
filenames = []
for filename in os.listdir(path):
if not filename.endswith('.xml'):
continue
fullname = os.path.join(path,filename)
filenames.append(fullname)
each_name = filename[:-4]
with open(each_name + '.txt', 'a') as f:
for filename in filenames:
tree = ET.parse(filename)
root = tree.getroot()
for object in root.findall('object'):
categoryID = object.find('name').text
for bnd_box in object.findall('bndbox'):
Xcenter = (int(bnd_box.find('xmax').text) - int(bnd_box.find('xmin').text))/2
Ycenter = (int(bnd_box.find('ymax').text) - int(bnd_box.find('ymin').text))/2
width = int(bnd_box.find('xmax').text)- int(bnd_box.find('xmin').text)
height = int(bnd_box.find('ymax').text) - int(bnd_box.find('ymin').text)
Detection_rows = str(categoryID) + str(Xcenter) + str(Ycenter) + str(width) + str(height) + '\n'
f.write(str(Detection_rows))
非常感谢
试试这个(没测试)
from xml.etree import ElementTree as ET
import os
path = r'/Users/mo/Documents/annotations/xmls'
for filename in os.listdir(path):
if not filename.endswith('.xml'):
continue
fullname = os.path.join(path, filename)
with open(fullname[:-4] + '.txt', 'a') as f:
tree = ET.parse(fullname)
root = tree.getroot()
for object in root.findall('object'):
categoryID = object.find('name').text
for bnd_box in object.findall('bndbox'):
Xcenter = (int(bnd_box.find('xmax').text) - int(bnd_box.find('xmin').text))/2
Ycenter = (int(bnd_box.find('ymax').text) - int(bnd_box.find('ymin').text))/2
width = int(bnd_box.find('xmax').text)- int(bnd_box.find('xmin').text)
height = int(bnd_box.find('ymax').text) - int(bnd_box.find('ymin').text)
Detection_rows = str(categoryID) + str(Xcenter) + str(Ycenter) + str(width) + str(height) + '\n'
f.write(str(Detection_rows))
您的 for 循环应该在 open
之外并且您将 filenames
更改为 filename
。另外,我删除了一个不必要的 for 循环。如果它能解决您的问题,请告诉我。