ubuntu系统 python代码将.txt文件转为.xml文件

多个文件.txt文件转为一个.xml文件:

#! /usr/bin/python

import os, sys
import glob
import xml.dom.minidom
from PIL import Image

src_txt_dir = "/home/lrj/picture/ICDAR2013/Challenge2_Test_Task1_GT/"
img_basenames = [] # e.g. 100.jpg


#for item in img_basenames:
fileList=os.listdir(src_txt_dir)
xml_file=open((src_txt_dir+'gt.xml'), 'w')
xml_file.write('<?xml version="1.0" encoding="UTF-8"?>\n')
xml_file.write('<tagset>\n')
total_num=len(fileList)
idx=range(total_num-1)
i=1
# control the number of txt
for i in idx:
    stri='%d'%i
    gt = open(src_txt_dir + 'gt_img_' + stri+ '.txt').read().splitlines()
    xml_file.write('    <image>\n')
    xml_file.write('    <imageName>'+'img_'+stri+'.jpg'+'</imageName>\n')
    xml_file.write('      <taggedRectangles>\n')
    for img_each_label in gt:
        spt = img_each_label.split(',')
        width=int(str(spt[2]))-int(str(spt[0]))
        w='%d'%width
        height=int(str(spt[3]))-int(str(spt[1]))
        h='%d'%height
        xml_file.write('            <taggedRectangle x='+'"'+str(spt[0])+'"'+  '  y='+'"'+str(spt[1])+'"'+'  width='+'"'+w+'"'+'  height='+'"'+h+'"' +'  offset='+'"'+'%d'%0 + '"'+' />\n')
    xml_file.write('      </taggedRectangles>\n')
    xml_file.write('    </image>\n')
xml_file.write('</tagset>\n')



猜你喜欢

转载自blog.csdn.net/feelingjun/article/details/70262076