【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运行中频繁出现Restart提示的解决办法

《Python运行中频繁出现Restart提示的解决办法》在编程的世界里,遇到各种奇怪的问题是家常便饭,但是,当你的Python程序在运行过程中频繁出现“Restart”提示时,这可能不仅仅是令人头疼... 目录问题描述代码示例无限循环递归调用内存泄漏解决方案1. 检查代码逻辑无限循环递归调用内存泄漏2.

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

python uv包管理小结

《pythonuv包管理小结》uv是一个高性能的Python包管理工具,它不仅能够高效地处理包管理和依赖解析,还提供了对Python版本管理的支持,本文主要介绍了pythonuv包管理小结,具有一... 目录安装 uv使用 uv 管理 python 版本安装指定版本的 Python查看已安装的 Python

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Python中局部变量和全局变量举例详解

《Python中局部变量和全局变量举例详解》:本文主要介绍如何通过一个简单的Python代码示例来解释命名空间和作用域的概念,它详细说明了内置名称、全局名称、局部名称以及它们之间的查找顺序,文中通... 目录引入例子拆解源码运行结果如下图代码解析 python3命名空间和作用域命名空间命名空间查找顺序命名空

Python如何将大TXT文件分割成4KB小文件

《Python如何将大TXT文件分割成4KB小文件》处理大文本文件是程序员经常遇到的挑战,特别是当我们需要把一个几百MB甚至几个GB的TXT文件分割成小块时,下面我们来聊聊如何用Python自动完成这... 目录为什么需要分割TXT文件基础版:按行分割进阶版:精确控制文件大小完美解决方案:支持UTF-8编码