116.python处理xmind文件

0.需求产生原因

我们项目需要一个思维导图的模块,通过前段上传xmind文件,后端解析xmind文件数据为json格式返回给前端,但是前端的kityminder这个插件产生的数据和后端使用的第三方包读取数据的格式不一样,所以有了下面的代码.

1.xmind转换格式

from xmindparser import xmind_to_dict
xmind_file = "/root/test.xmind"
out_file = xmind_to_dict(xmind_file)  # 这个直接输出字段格式数据
print(out_file)

xmind_file = "/root/test.xmind"
out_file = xmind_to_json(xmind_file)  # 这个很奇怪他直接同级目录生成同名.json文件.
这个第三方包很简单直接看源码很方便

2.前段使用的kityminder需要的格式

{"root":{"data":{"id":"c7iz2u1vbxk0","created":1606980595399,"text":"自动化测试平台"},"children":[{"data":{"id":"c7iz2vexxv40","created":1606980598366,"text":"实时系统监控"},"children":[{"data":{"id":"c7izjttyb4o0","created":1606981927111,"text":"测试项目实时数据展示"},"children":[]},{"data":{"id":"c7izjuyfpw80","created":1606981929559,"text":"内存等信息"},"children":[]}]},{"data":{"id":"c7iz2z92fgg0","created":1606980606718,"text":"测试工具"},"children":[{"data":{"id":"c7izjvqiwew0","created":1606981931257,"text":"终端产品工具"},"children":[]},{"data":{"id":"c7izjwmj0ug0","created":1606981933192,"text":"其他产品常用测试工具"},"children":[]}]},{"data":{"id":"c7izjq28a540","created":1606981918904,"text":"测试环境"},"children":[{"data":{"id":"c7izkzsh6e80","created":1606982018447,"text":"IP地址"},"children":[]},{"data":{"id":"c7izl0u6tj40","created":1606982020727,"text":"port端口"},"children":[]},{"data":{"id":"c7izl1gk4340","created":1606982022079,"text":"user用户名"},"children":[]},{"data":{"id":"c7izl22dwcg0","created":1606982023399,"text":"PassWord"},"children":[]}]}]},"template":"default","theme":"fresh-blue","version":"1.4.43"}

3.通过1中的工具我拿到的数据格式

(通过python第三方包xmindparser读取上传的xmind文件拿到的数据)

[{'topic': {'title': '自动化测试平台', 'topics': [{'title': '实时监控系统', 'topics': [{'title': '测试项目实时数据展示'}, {'title': '内存等信息'}]}, {'title': '测试工具', 'topics': [{'title': '终端产品工具'}, {'title': '其它产品常用测试工具'}]}, {'title': '测试环境', 'topics': [{'title': 'IP地址'}, {'title': 'port端口号'}, {'title': 'user用户名'}, {'title': 'PassWord'}]}]}}]

4.代码实现

我需要将3中的格式转换为2中前段可以识别的格式,由于我发现我现有的python知识解决不了它了(我使用了递归解决他,但是前段告诉我思维导图的节点值可能重复,所以我需要一种结构来存储它,刚好我正在学习数据结构,因此我想到了树这种结构),我自己设计了一个树的数据类型,将数据初始化进入树种,之后通过递归遍历树形结构转换获取数据.(如果有什么好的想法可以评论,我知道我写的这个效率应该不是很高)

import time
import json

class Tree(object):
    root = None

    def show(self):
        if self.root != None:
            self.root.show()

    def templete(self, node):
        templete = {
            "data": {
                "id": "c7izjuyfpw80",
                "created": int(time.mktime(time.localtime()) * 1000),
                "text": node.title
            },
            "children": []
        }
        return templete

    def create_xmind(self, data_dict, node):
        for child in node.children:
            child_dict = self.templete(child)
            data_dict["children"].append(child_dict)
            self.create_xmind(child_dict, child)
        return data_dict

    def get_data(self):
        root_dict = {
            "root": None,
            "template": "default",
            "theme": "fresh-blue",
            "version": "1.4.43"
        }
        data_dict = self.create_xmind(self.templete(self.root), self.root)
        root_dict["root"] = data_dict
        return root_dict

    def init_data(self, data):
        if data:
            self.root = Node(data[0]["topic"]["title"])
            self.add_children(self.root, data[0]["topic"]["topics"])

    def add_children(self, parent, data):
        for item in data:
            title = item["title"]
            node = Node(title)
            parent.add(node)
            if item.get("topics", None):
                self.add_children(node, item.get("topics"))


class Node(object):

    def __init__(self, title):
        self.title = title
        self.children = []

    def add(self, node):
        if self.title != None:
            self.children.append(node)

    def show(self):
        print(self.title)
        if self.children:
            for item in self.children:
                item.show()


d1 = [{
	'topic': {
		'title': '自动化测试平台',
		'topics': [{
			'title': '实时监控系统',
			'topics': [{
				'title': '测试项目实时数据展示'
			}, {
				'title': '内存等信息'
			}]
		}, {
			'title': '测试工具',
			'topics': [{
				'title': '终端产品工具'
			}, {
				'title': '其它产品常用测试工具'
			}]
		}, {
			'title': '测试环境',
			'topics': [{
				'title': 'IP地址'
			}, {
				'title': 'port端口号'
			}, {
				'title': 'user用户名'
			}, {
				'title': 'PassWord'
			}]
		}]
	}
}]

tree = Tree()

tree.init_data(d1)
print(json.dumps(tree.get_data()))


""" 结果
{
	'root': {
		'data': {
			'id': 'c7izjuyfpw80',
			'created': 1607061896000,
			'text': '自动化测试平台'
		},
		'children': [{
			'data': {
				'id': 'c7izjuyfpw80',
				'created': 1607061896000,
				'text': '实时监控系统'
			},
			'children': [{
				'data': {
					'id': 'c7izjuyfpw80',
					'created': 1607061896000,
					'text': '测试项目实时数据展示'
				},
				'children': []
			}, {
				'data': {
					'id': 'c7izjuyfpw80',
					'created': 1607061896000,
					'text': '内存等信息'
				},
				'children': []
			}]
		}, {
			'data': {
				'id': 'c7izjuyfpw80',
				'created': 1607061896000,
				'text': '测试工具'
			},
			'children': [{
				'data': {
					'id': 'c7izjuyfpw80',
					'created': 1607061896000,
					'text': '终端产品工具'
				},
				'children': []
			}, {
				'data': {
					'id': 'c7izjuyfpw80',
					'created': 1607061896000,
					'text': '其它产品常用测试工具'
				},
				'children': []
			}]
		}, {
			'data': {
				'id': 'c7izjuyfpw80',
				'created': 1607061896000,
				'text': '测试环境'
			},
			'children': [{
				'data': {
					'id': 'c7izjuyfpw80',
					'created': 1607061896000,
					'text': 'IP地址'
				},
				'children': []
			}, {
				'data': {
					'id': 'c7izjuyfpw80',
					'created': 1607061896000,
					'text': 'port端口号'
				},
				'children': []
			}, {
				'data': {
					'id': 'c7izjuyfpw80',
					'created': 1607061896000,
					'text': 'user用户名'
				},
				'children': []
			}, {
				'data': {
					'id': 'c7izjuyfpw80',
					'created': 1607061896000,
					'text': 'PassWord'
				},
				'children': []
			}]
		}]
	},
	'template': 'default',
	'theme': 'fresh-blue',
	'version': '1.4.43'
}
"""




原文地址:https://www.cnblogs.com/liuzhanghao/p/14085607.html