Python TXT 转为XML文件

本文介绍如何使用Python的xml.dom.minidom模块创建XML文档。详细解释了Document对象及其方法,如createElement()、createTextNode()和setAttribute()等,并通过示例展示了如何构建完整的XML文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XML文档

Document 对象
Document 对象是一棵文档树的根,可为我们提供对文档数据的最初(或最顶层)的访问入口。
用于元素节点、文本节点、注释、处理指令等均无法存在于 document 之外,document 对象同样提供了创建这些对象的方法。Node 对象提供了一个 ownerDocument 属性,此属性可把它们与在其中创建它们的 Document 关联起来。
createElement() 方法可创建元素节点。
此方法可返回一个 Element 对象。
语法

createElement(name)
  • 参数 : name
  • 描述 :字符串值,这个字符串可为此元素节点规定名称。

createTextNode() 可创建文本节点。节点的标签值(data)
此方法可返回 Text 对象
语法

createTextNode(data)
  • 参数 :data
  • 描述 :字符串值,可规定此节点的文本。

setAttribute()给节点添加属性值(Attribute)

node.setAttribute("att_name", "arr_value")

在创建完节点(节点值)之后,还需使用下面的方法添加到指点的节点的位置下面:

prev_node.appendChild(cur_node)

这里的prev_node要添加节点的上一层节点,而cur_node即为当前要添加的节点了

遇到问题:
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe8 in position 54: ordinal not in range(128)
解决方法:
原因就是python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误。在代码中加上如下几句即可。

import sys
reload(sys)
sys.setdefaultencoding('utf8')

其他更多XML Document对象函数,请查看:
http://www.w3school.com.cn/xmldom/dom_document.asp


代码:

# -*- coding: utf-8 -*-

from xml.dom.minidom import Document
import os
import os.path
from PIL import Image
import sys
reload(sys)
sys.setdefaultencoding('utf8')

ann_path = "/home/ubuntu/Downloads/txt_9000/"
img_path = "/home/ubuntu/Downloads/image_9000/"
xml_path = "/home/ubuntu/Downloads/label_9000/"

if not os.path.exists(xml_path):
    os.mkdir(xml_path)


def writeXml(tmp, imgname, w, h, objbud, wxml):
    doc = Document()
    # owner
    annotation = doc.createElement('annotation')
    doc.appendChild(annotation)
    # owner
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder_txt = doc.createTextNode("VOC2007")
    folder.appendChild(folder_txt)

    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename_txt = doc.createTextNode(imgname)
    filename.appendChild(filename_txt)
    # ones#
    source = doc.createElement('source')
    annotation.appendChild(source)

    database = doc.createElement('database')
    source.appendChild(database)
    database_txt = doc.createTextNode("The VOC2007 Database")
    database.appendChild(database_txt)

    annotation_new = doc.createElement('annotation')
    source.appendChild(annotation_new)
    annotation_new_txt = doc.createTextNode("PASCAL VOC2007 ")
    annotation_new.appendChild(annotation_new_txt)

    image = doc.createElement('image')
    source.appendChild(image)
    image_txt = doc.createTextNode("flickr")
    image.appendChild(image_txt)
    # onee#
    # twos#
    size = doc.createElement('size')
    annotation.appendChild(size)

    width = doc.createElement('width')
    size.appendChild(width)
    width_txt = doc.createTextNode(str(w))
    width.appendChild(width_txt)

    height = doc.createElement('height')
    size.appendChild(height)
    height_txt = doc.createTextNode(str(h))
    height.appendChild(height_txt)

    depth = doc.createElement('depth')
    size.appendChild(depth)
    depth_txt = doc.createTextNode("3")
    depth.appendChild(depth_txt)
    # twoe#
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented_txt = doc.createTextNode("0")
    segmented.appendChild(segmented_txt)

    for i in range(0, len(objbud) / 5):
        # threes#
        object_new = doc.createElement("object")
        annotation.appendChild(object_new)

        name = doc.createElement('name')
        object_new.appendChild(name)
        name_txt = doc.createTextNode(objbud[i * 5])
        name.appendChild(name_txt)

        pose = doc.createElement('pose')
        object_new.appendChild(pose)
        pose_txt = doc.createTextNode("Unspecified")
        pose.appendChild(pose_txt)

        truncated = doc.createElement('truncated')
        object_new.appendChild(truncated)
        truncated_txt = doc.createTextNode("0")
        truncated.appendChild(truncated_txt)

        difficult = doc.createElement('difficult')
        object_new.appendChild(difficult)
        difficult_txt = doc.createTextNode("0")
        difficult.appendChild(difficult_txt)
        # threes-1#
        bndbox = doc.createElement('bndbox')
        object_new.appendChild(bndbox)

        xmin = doc.createElement('xmin')
        bndbox.appendChild(xmin)
        xmin_txt = doc.createTextNode(objbud[i * 5 + 1])
        xmin.appendChild(xmin_txt)

        ymin = doc.createElement('ymin')
        bndbox.appendChild(ymin)
        ymin_txt = doc.createTextNode(objbud[i * 5 + 2])
        ymin.appendChild(ymin_txt)

        xmax = doc.createElement('xmax')
        bndbox.appendChild(xmax)
        xmax_txt = doc.createTextNode(objbud[i * 5 + 3])
        xmax.appendChild(xmax_txt)

        ymax = doc.createElement('ymax')
        bndbox.appendChild(ymax)
        ymax_txt = doc.createTextNode(objbud[i * 5 + 4])
        ymax.appendChild(ymax_txt)
        # threee-1#
        # threee#

    tempfile = tmp + "test.xml"
    with open(tempfile, "w") as f:
        f.write(doc.toprettyxml(indent="\t", newl="\n", encoding="utf-8"))

    rewrite = open(tempfile, "r")
    lines = rewrite.read().split('\n')
    newlines = lines[1:len(lines) - 1]

    fw = open(wxml, "w")
    for i in range(0, len(newlines)):
        fw.write(newlines[i] + '\n')

    fw.close()
    rewrite.close()
    os.remove(tempfile)
    return


for files in os.walk(ann_path):
    temp = "/home/ubuntu/temp/"
    if not os.path.exists(temp):
        os.mkdir(temp)
    for file in files[2]:
        print file + "-->start!"
        img_name = os.path.splitext(file)[0] + '.jpg'
        fileimgpath = img_path + img_name
        im = Image.open(fileimgpath)
        width = int(im.size[0])
        height = int(im.size[1])

        filelabel = open(ann_path + file, "r")
        lines = filelabel.read().split('\n')
        obj = lines[:len(lines) - 1]

        filename = xml_path + os.path.splitext(file)[0] + '.xml'
        writeXml(temp, img_name, width, height, obj, filename)
    os.rmdir(temp)

参考文献:https://blog.youkuaiyun.com/samylee/article/details/62040727

### 将TXT文件转换为XML格式的方法 对于将YOLOv5推理保存的TXT文本格式转换成更常用的XML格式,可以采用Python编写专门的脚本来完成这一任务。下面是一个具体的实现方案[^3]。 #### 准备工作 确保安装了必要的库,如`lxml`,它提供了强大的API来创建和解析XML文档。 ```bash pip install lxml ``` #### Python代码示例 这里给出一段完整的Python代码片段,该程序读取指定目录下的所有`.txt`文件,并将其内容按照Pascal VOC标准写入到对应的`.xml`文件中: ```python from lxml import etree as ET import os def txt_to_xml(txt_path, xml_dir): with open(txt_path, 'r') as file: lines = file.readlines() filename = os.path.basename(txt_path).replace('.txt', '.jpg') folder_name = "VOC2007" annotation = ET.Element('annotation') # 构建基础节点 ET.SubElement(annotation, 'folder').text = folder_name ET.SubElement(annotation, 'filename').text = filename source = ET.SubElement(annotation, 'source') ET.SubElement(source, 'database').text = 'Unknown' size_part = ET.SubElement(annotation, 'size') ET.SubElement(size_part, 'width').text = str(640) # 假设图像宽度为640像素 ET.SubElement(size_part, 'height').text = str(480) # 假设图像高度为480像素 ET.SubElement(size_part, 'depth').text = str(3) segmented = ET.SubElement(annotation, 'segmented') segmented.text = '0' for line in lines: parts = line.strip().split() # YOLO格式: 类别 中心X 中心Y 宽度 高度 object_item = ET.SubElement(annotation, 'object') name_tag = ET.SubElement(object_item, 'name') pose_tag = ET.SubElement(object_item, 'pose') truncated_tag = ET.SubElement(object_item, 'truncated') difficult_tag = ET.SubElement(object_item, 'difficult') bndbox_tag = ET.SubElement(object_item, 'bndbox') class_id = int(parts[0]) center_x = float(parts[1]) * 640 # 调整回原图尺寸的比例 center_y = float(parts[2]) * 480 bbox_width = float(parts[3]) * 640 bbox_height = float(parts[4]) * 480 xmin = max(int(center_x - (bbox_width / 2)), 0) ymin = max(int(center_y - (bbox_height / 2)), 0) xmax = min(int(center_x + (bbox_width / 2)), 640) ymax = min(int(center_y + (bbox_height / 2)), 480) name_tag.text = f"class_{class_id}" # 替换为你实际使用的标签名称 pose_tag.text = "Unspecified" truncated_tag.text = "0" difficult_tag.text = "0" ET.SubElement(bndbox_tag, 'xmin').text = str(xmin) ET.SubElement(bndbox_tag, 'ymin').text = str(ymin) ET.SubElement(bndbox_tag, 'xmax').text = str(xmax) ET.SubElement(bndbox_tag, 'ymax').text = str(ymax) tree = ET.ElementTree(annotation) output_file = os.path.join(xml_dir, os.path.splitext(os.path.basename(txt_path))[0]+'.xml') tree.write(output_file, pretty_print=True) if __name__ == '__main__': input_folder = './labels' # 存放.txt文件夹的位置 output_folder = './annotations' # 输出.xml文件夹位置 if not os.path.exists(output_folder): os.makedirs(output_folder) for root, dirs, files in os.walk(input_folder): for file in files: if file.endswith(".txt"): fullpath = os.path.join(root, file) txt_to_xml(fullpath, output_folder) ``` 这段代码遍历给定文件夹中的每一个`.txt`文件,根据其内部的数据构建相应的XML结构,并最终保存至另一个指定的目标文件夹内。注意,在此过程中假设所有的图片都是固定大小(例如640x480),如果实际情况不同,则需相应调整这部分逻辑以适应不同的输入条件[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值