【Python】把指定组织形式的txt转换为xmind

2024-06-07 06:04

本文主要是介绍【Python】把指定组织形式的txt转换为xmind,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

人工智能训练通常需要使用文本格式,把基于训练之后的内容,让GLM大模型输出如下格式的文本:

weltestDemo|#|weltest|#|静态界面|#|输入|#|长度|#|不超过四位
weltestDemo|#|weltest|#|静态界面|#|输入|#|长度|#|不超过五位
weltestDemo|#|weltest|#|静态界面|#|输入|#|长度|#|不超过6位
weltestDemo|#|weltest|#|静态界面|#|输入2|#|不超过四位
weltestDemo|#|weltest|#|静态界面|#|输入2|#|不超过五位
weltestDemo|#|weltest|#|静态界面|#|输入2|#|不超过6位
weltestDemo|#|weltest|#|功能测试|#|输入|#|长度|#|不超过四位
weltestDemo|#|weltest|#|功能测试|#|输入|#|长度|#|不超过五位
weltestDemo|#|weltest|#|功能测试|#|输入|#|长度|#|不超过6位
weltestDemo|#|weltest|#|功能测试|#|输入2|#|不超过四位
weltestDemo|#|weltest|#|功能测试|#|输入2|#|不超过五位
weltestDemo|#|weltest|#|功能测试|#|输入2|#|不超过6位

如何把上述文本转换为xmind格式呢?

软件信息

python

python -v

Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
依赖库

pip install XMind==1.2.0

xmind版本

XMind 8 Update 7 (R3.7.7.201801311814)

在这里插入图片描述

程序脚本

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import os,re
import xmind
def txt2Xmind(txt_file=r'weltestDemo.txt'):filename,_=os.path.splitext(os.path.basename(txt_file))print(filename)with open(txt_file,'r',encoding='UTF-8') as xfile:lines=xfile.readlines()if os.path.exists(f"{filename}.xmind"):os.remove(f"{filename}.xmind")wk = xmind.load(f"{filename}.xmind")rootset = lines[0].strip('\n').split('|#|')picture = wk.getPrimarySheet()picture.setTitle(rootset[0])root = picture.getRootTopic()root.setTitle(rootset[1])preitems=lines[0].strip('\n').split('|#|')current_topic=rootfor line in lines:items=line.strip('\n').split('|#|')last_index=len(set(items)&set(preitems))diff_record=len(set(items)-set(preitems))current_length=len(set(items))print(str(items)+str(current_length)+'items')print(str(preitems)+'preitems')#print(str(set(items)&set(preitems))+'inter')expect_topic_title=items[last_index-1]#print(current_topic.getTitle()+'root')if diff_record==0:for sub in items[2:]:current_topic=current_topic.addSubTopic()current_topic.setTitle(sub)else:root_index=preitems.index(root.getTitle())expect_index=preitems.index(expect_topic_title)#距离根节点的距离distance=expect_index-root_indextmp = rootfor i in range(1,distance+1):subtopics=[item for item  in tmp.getSubTopics() if item.getTitle()==preitems[root_index+i]]tmp=subtopics[0]print(tmp.getTitle())current_topic=tmpfor sub in items[current_length-diff_record:]:current_topic=current_topic.addSubTopic()current_topic.setTitle(sub)preitems = itemsxmind.save(wk, f"{filename}.xmind")if __name__=="__main__":txt2Xmind()

操作演示

weltestDemo.txt
weltestDemo|#|weltest|#|静态界面|#|输入|#|长度|#|不超过四位
weltestDemo|#|weltest|#|静态界面|#|输入|#|长度|#|不超过五位
weltestDemo|#|weltest|#|静态界面|#|输入|#|长度|#|不超过6位
weltestDemo|#|weltest|#|静态界面|#|输入2|#|不超过四位
weltestDemo|#|weltest|#|静态界面|#|输入2|#|不超过五位
weltestDemo|#|weltest|#|静态界面|#|输入2|#|不超过6位
weltestDemo|#|weltest|#|功能测试|#|输入|#|长度|#|不超过四位
weltestDemo|#|weltest|#|功能测试|#|输入|#|长度|#|不超过五位
weltestDemo|#|weltest|#|功能测试|#|输入|#|长度|#|不超过6位
weltestDemo|#|weltest|#|功能测试|#|输入2|#|不超过四位
weltestDemo|#|weltest|#|功能测试|#|输入2|#|不超过五位
weltestDemo|#|weltest|#|功能测试|#|输入2|#|不超过6位
执行命令

调整脚本中文件名称为“weltestDemo.txt”,可以依据自己需要进行调整

(venv) PS D:\Python\xmind> python .\txt2xmind.py
weltestDemo
['weltestDemo', 'weltest', '静态界面', '输入', '长度', '不超过四位']6items
['weltestDemo', 'weltest', '静态界面', '输入', '长度', '不超过四位']preitems
['weltestDemo', 'weltest', '静态界面', '输入', '长度', '不超过五位']6items
['weltestDemo', 'weltest', '静态界面', '输入', '长度', '不超过四位']preitems
静态界面
输入
长度
['weltestDemo', 'weltest', '静态界面', '输入', '长度', '不超过6位']6items
['weltestDemo', 'weltest', '静态界面', '输入', '长度', '不超过五位']preitems
静态界面
输入
长度
['weltestDemo', 'weltest', '静态界面', '输入2', '不超过四位']5items
['weltestDemo', 'weltest', '静态界面', '输入', '长度', '不超过6位']preitems
静态界面
['weltestDemo', 'weltest', '静态界面', '输入2', '不超过五位']5items
['weltestDemo', 'weltest', '静态界面', '输入2', '不超过四位']preitems
静态界面
输入2
['weltestDemo', 'weltest', '静态界面', '输入2', '不超过6位']5items
['weltestDemo', 'weltest', '静态界面', '输入2', '不超过五位']preitems
静态界面
输入2
['weltestDemo', 'weltest', '功能测试', '输入', '长度', '不超过四位']6items
['weltestDemo', 'weltest', '静态界面', '输入2', '不超过6位']preitems
['weltestDemo', 'weltest', '功能测试', '输入', '长度', '不超过五位']6items
['weltestDemo', 'weltest', '功能测试', '输入', '长度', '不超过四位']preitems
功能测试
输入
长度
['weltestDemo', 'weltest', '功能测试', '输入', '长度', '不超过6位']6items
['weltestDemo', 'weltest', '功能测试', '输入', '长度', '不超过五位']preitems
功能测试
输入
长度
['weltestDemo', 'weltest', '功能测试', '输入2', '不超过四位']5items
['weltestDemo', 'weltest', '功能测试', '输入', '长度', '不超过6位']preitems
功能测试
['weltestDemo', 'weltest', '功能测试', '输入2', '不超过五位']5items
['weltestDemo', 'weltest', '功能测试', '输入2', '不超过四位']preitems
功能测试
输入2
['weltestDemo', 'weltest', '功能测试', '输入2', '不超过6位']5items
['weltestDemo', 'weltest', '功能测试', '输入2', '不超过五位']preitems
功能测试
输入2
(venv) PS D:\Python\xmind>
最后结果

在这里插入图片描述

现存问题

由下面两行代码实现,引入了一个问题:画布名称和主题名称不能一致,不然会提示越界情况

root_index=preitems.index(root.getTitle())
expect_index=preitems.index(expect_topic_title)

这篇关于【Python】把指定组织形式的txt转换为xmind的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1038350

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

基于Python开发一个图像水印批量添加工具

《基于Python开发一个图像水印批量添加工具》在当今数字化内容爆炸式增长的时代,图像版权保护已成为创作者和企业的核心需求,本方案将详细介绍一个基于PythonPIL库的工业级图像水印解决方案,有需要... 目录一、系统架构设计1.1 整体处理流程1.2 类结构设计(扩展版本)二、核心算法深入解析2.1 自

从入门到进阶讲解Python自动化Playwright实战指南

《从入门到进阶讲解Python自动化Playwright实战指南》Playwright是针对Python语言的纯自动化工具,它可以通过单个API自动执行Chromium,Firefox和WebKit... 目录Playwright 简介核心优势安装步骤观点与案例结合Playwright 核心功能从零开始学习

Python 字典 (Dictionary)使用详解

《Python字典(Dictionary)使用详解》字典是python中最重要,最常用的数据结构之一,它提供了高效的键值对存储和查找能力,:本文主要介绍Python字典(Dictionary)... 目录字典1.基本特性2.创建字典3.访问元素4.修改字典5.删除元素6.字典遍历7.字典的高级特性默认字典

Python自动化批量重命名与整理文件系统

《Python自动化批量重命名与整理文件系统》这篇文章主要为大家详细介绍了如何使用Python实现一个强大的文件批量重命名与整理工具,帮助开发者自动化这一繁琐过程,有需要的小伙伴可以了解下... 目录简介环境准备项目功能概述代码详细解析1. 导入必要的库2. 配置参数设置3. 创建日志系统4. 安全文件名处

使用Python构建一个高效的日志处理系统

《使用Python构建一个高效的日志处理系统》这篇文章主要为大家详细讲解了如何使用Python开发一个专业的日志分析工具,能够自动化处理、分析和可视化各类日志文件,大幅提升运维效率,需要的可以了解下... 目录环境准备工具功能概述完整代码实现代码深度解析1. 类设计与初始化2. 日志解析核心逻辑3. 文件处

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定