python-如何给乱序的xml文件重新添加字符间隔符号

发布时间:2024年01月15日

?没有字符间隔符号的xml文件:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import xml.etree.ElementTree as ET
from xml.dom import minidom
import os

###### 添加字符间距格式 ##########
def add_indentation(input_file, output_file):
    # 读取XML文件并解析为ElementTree
    tree = ET.parse(input_file)
    root = tree.getroot()
    # 使用minidom将ElementTree转换为带有缩进和格式的XML字符串
    xml_str = minidom.parseString(ET.tostring(root)).toprettyxml(indent="  ")
    # 将格式化的XML字符串写入新的XML文件
    # print(output_file)
    with open(output_file, "w") as f:
        f.write(xml_str)

###### 判断是否含有字符间距格式 #########
def is_formatted(xml_file):
    # 解析XML文件
    tree = ET.parse(xml_file)
    root = tree.getroot()
    # 获取XML字符串
    xml_str = ET.tostring(root, encoding="utf-8").decode("utf-8")
    # 判断XML字符串是否包含缩进和换行符
    return '\n' in xml_str or '\t' in xml_str


###### 没有字符间距格式的xml文件添加字符间距格式 #########
def convert_annotation(xml_path,save_xml,image_id):
    if not is_formatted(xml_path+'/%s.xml' % (image_id)):
        print(xml_path+'/%s.xml' % (image_id))
        add_indentation(xml_path+'/%s.xml' % (image_id), save_xml+'/%s.xml' % (image_id))


xml_path = '/home/gyx/git/data/detect/Vehicle_Detection/val/val/vallabel/'
save_txt='/home/gyx/git/data/detect/Vehicle_Detection/val/val/vallabel_new/'

# xml list
img_xmls = os.listdir(xml_path)
for img_xml in img_xmls:
    label_name = img_xml.split('.')[0]
    # print(label_name)
    convert_annotation(xml_path,save_txt,label_name)

添加了字符间隔符号的xml文件:?

文章来源:https://blog.csdn.net/weixin_40446557/article/details/135605109
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。