学习Python:将xml转换为txt/csv

将用labelImg.exe标注后生成的xml文件转换为方便处理的txt/csv,具体信息根据实际情况提取。

import os
import csv
import glob

def XML2TXT(xmlPath: str):
    dataList = []
    with open(xmlPath, 'r') as fp:
        for p in fp:
            if '' in p:
                fileName = p.split('>')[1].split('<')[0]
            if '' in p:
                d = [next(fp).split('>')[1].split('<')[0] for _ in range(9)]    # 类别
                strType = d[0]
                x1 = int(d[-4])     # 边界框
                y1 = int(d[-3])
                x2 = int(d[-2])
                y2 = int(d[-1])
                dataList.append([os.path.join('JPEGImages', fileName), x1, y1, x2, y2, strType])
    txtPath = xmlPath.replace(".xml", ".txt")   # or csv
    with open(txtPath, 'w', newline='') as fp:
        csv_writer = csv.writer(fp, dialect='excel')
        csv_writer.writerows(dataList)

path = glob.glob('*.xml')
for xmlFile in path:
    XML2TXT(xmlFile) 
  

以下为labelimg生成的xml文件


	8bit
	xxx.png
	D:/xxx.png
	
		Unknown
	
	
		2656
		5310
		1
	
	0
	
		pos_screen_b5
		Unspecified
		0
		0
		
			128
			672
			179
			842
		
	
	
		pos_screen_b4
		Unspecified
		0
		0
		
			132
			3550
			180
			3735
		
	
	
		pos_screen_b1
		Unspecified
		0
		0
		
			2450
			4450
			2505
			4631
		
	
	
		pos_screen_b2
		Unspecified
		0
		0
		
			2447
			3438
			2507
			3615
		
	
	
		pos_screen_b3
		Unspecified
		0
		0
		
			2456
			1965
			2516
			2142
		
	

转换结果

JPEGImages\xxx.png,128,672,179,842,pos_screen_b5
JPEGImages\xxx.png,132,3550,180,3735,pos_screen_b4
JPEGImages\xxx.png,2450,4450,2505,4631,pos_screen_b1
JPEGImages\xxx.png,2447,3438,2507,3615,pos_screen_b2
JPEGImages\xxx.png,2456,1965,2516,2142,pos_screen_b3

 

你可能感兴趣的:(学习Python,python,xml)