read txt file and new xml file with python

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Zzhouzhou237/article/details/83856841
import xml.dom.minidom
import os

FILE_PATH= 'C:\\Users\\503061752\\Desktop\\XmlFiles'//生成.xml文件路径
if not os.path.exists(FILE_PATH)://如果文件夹目录不存在,则新建

        os.makedirs(FILE_PATH)

file = open("Label.txt")//打开txt文件

//将目标内容放入lines中
lines = []
while 1:
  curline = file.readline()
  if not curline:
    break
  else:
    if 'Traffic Light' in curline:
      curline = curline.strip('\n')
      lines.append(curline)
//label.txt文件中有内容是部分重复的,所以要合成一个xml文件输出
i = 0
while i < len(lines):
  intrepeat = 1
  while (i + 1) < len(lines) and  (lines[i].split(' '))[2] == (lines[i + 1].split(' '))[2]:
    intrepeat = intrepeat + 1
    i = i + 1
  line = lines[i]
  linecontent = line.split(' ')
 
 

  doc = xml.dom.minidom.Document()
  root = doc.createElement('annotation')
  doc.appendChild(root)

  folderNode = doc.createElement('folder')
  folderNode.appendChild(doc.createTextNode(str('XmlFiles')))
  root.appendChild(folderNode)

  filenameNode = doc.createElement('filename')
  strfilename = linecontent[2] + '.jpg'
  filenameNode.appendChild(doc.createTextNode(strfilename))
  root.appendChild(filenameNode)

  sizeNode = doc.createElement('size')
  widthNode = doc.createElement('width')
  widthNode.appendChild(doc.createTextNode(str('640')))
  sizeNode.appendChild(widthNode)
  heightNode = doc.createElement('height')
  heightNode.appendChild(doc.createTextNode(str('480')))
  sizeNode.appendChild(heightNode)
  depthNode = doc.createElement('depth')
  depthNode.appendChild(doc.createTextNode(str('3')))
  sizeNode.appendChild(depthNode)
  root.appendChild(sizeNode)


  for j in range(intrepeat):
    objectNode = doc.createElement('object')
    root.appendChild(objectNode)
    repeatline = lines[i - j].split(' ')
 
    subtypeNode = doc.createElement('subtype')
    subtypeNode.appendChild(doc.createTextNode(repeatline[10]))
    objectNode.appendChild(subtypeNode)

    bndboxNode = doc.createElement('bndbox')
    xminNode = doc.createElement('xmin')
    xminNode.appendChild(doc.createTextNode(repeatline[3]))
    bndboxNode.appendChild(xminNode)
    yminNode = doc.createElement('ymin')
    yminNode.appendChild(doc.createTextNode(repeatline[4]))
    bndboxNode.appendChild(yminNode)
    xmaxNode = doc.createElement('xmax')
    xmaxNode.appendChild(doc.createTextNode(repeatline[5]))
    bndboxNode.appendChild(xmaxNode)
    ymaxNode = doc.createElement('ymax')
    ymaxNode.appendChild(doc.createTextNode(repeatline[6]))
    bndboxNode.appendChild(ymaxNode)
    objectNode.appendChild(bndboxNode)
  i = i +1
  strxmlname = linecontent[2] + '.xml'
  fp = open(FILE_PATH + '\\' + strxmlname, 'w')
  #doc.writexml(fp, indent='\t', addindent='\t', newl='\n')
  doc.writexml(fp, addindent='\t', newl='\n')

下面是部分label.txt内容

############################ PUBLIC DATABASE ############################
#http://www.lara.prd.fr/benchmarks/trafficlightsrecognition 
#(free of use for any research purpose)
#
#-----------------------------------------------------------------------#
#Database provided by the *Robotics Centre, of Mines ParisTech* (France)
#
#File format is as follows:
#Timestamp / frameindex x1 y1 x2 y2 id 'type' 'subtype'
#-----------------------------------------------------------------------#
#
#
#File version v 0.5
03:07.7172 / 772 498 93 504 108 0 'Traffic Light' 'go'
03:07.7758 / 773 498 93 504 108 0 'Traffic Light' 'go'
03:07.8338 / 774 498 94 504 109 0 'Traffic Light' 'go'
03:07.8926 / 775 498 94 504 109 0 'Traffic Light' 'go'
03:07.9505 / 776 499 95 505 110 0 'Traffic Light' 'go'
03:07.9505 / 776 499 95 505 110 0 'Traffic Light' 'go'

以上代码实现的功能是按行读取label.txt文件,并将每行不重复的内能新生成一个.xml文件。

猜你喜欢

转载自blog.csdn.net/Zzhouzhou237/article/details/83856841