前言
在将xmind文件转换成dict形式后我们需要将用到的数据提取到列表中,再对其进行操作。本文就简单讲述如何进行转换。
原数据格式
dict = {'id': 'dbeee450e48c607c6f7add552a', 'link': None, 'title': '11111', 'note': None, 'label': None, 'comment': None, 'markers': [],
'topics': [{'id': 'dbeee450e48c6da0141c3fd291', 'link': None, 'title': '1.1', 'note': None, 'label': None, 'comment': None, 'markers': [],
'topics': [{'id': 'dbeee450e48c6dadbc0a5a38e4', 'link': None, 'title': '1.11', 'note': None, 'label': None, 'comment': None, 'markers': []},
{'id': 'dbeee450e48c62508e5873e391', 'link': None, 'title': '1.12', 'note': None, 'label': None, 'comment': None, 'markers': []}]},
{'id': 'dbeee450e48c6787dbf279aa66', 'link': None, 'title': '1.2', 'note': None, 'label': None, 'comment': None, 'markers': [],
'topics': [{'id': 'dbeee450e48c63c5541a3ff859', 'link': None, 'title': '1.21', 'note': None, 'label': None, 'comment': None, 'markers': []},
{'id': 'dbeee450e48c6180628332bcfb', 'link': None, 'title': '1.22', 'note': None, 'label': None, 'comment': None, 'markers': []}]},
{'id': 'dbeee450e48c62a5890cf81683', 'link': None, 'title': '1.3', 'note': None, 'label': None, 'comment': None, 'markers': [],
'topics': [{'id': 'dbeee450e48c696f1130282c8d', 'link': None, 'title': '1.32', 'note': None, 'label': None, 'comment': None, 'markers': []},
{'id': 'dbeee450e48c6bd2ede4f91922', 'link': None, 'title': '1.31', 'note': None, 'label': None, 'comment': None, 'markers': []}]},
{'id': 'dbeee450e48c6e7b95d74f476d', 'link': None, 'title': '1.4', 'note': None, 'label': None, 'comment': None, 'markers': [],
'topics': [{'id': 'dbeee450e48c61325305d7c166', 'link': None, 'title': '1.41', 'note': None, 'label': None, 'comment': None, 'markers': []},
{'id': 'cf397084617f4700c721e66667', 'link': None, 'title': '1.42', 'note': None, 'label': None, 'comment': None, 'markers': []}]}]}
xmind图:
操作代码:
list = [dict['title'],]
s = []
m = []
for a in dict['topics']:
s = [a['title']]
k = []
for lens in range(len(a['topics'])):
k.append(a['topics'][lens]['title'])
s.append(k)
m.append(s)
list.append(m)
print(list)
得到的数据结果:
list = ['11111', [['1.1', ['1.11', '1.12']], ['1.2', ['1.21', '1.22']], ['1.3', ['1.32', '1.31']], ['1.4', ['1.41', '1.42']]]]
将list转换成dict
dict['title'] = list[0]
# print(list[1][0][0])
b = 0
for a in dict['topics']:
#替换二级title
a['title'] = list[1][b][0]
#替换三级title
for lens in range(len(a['topics'])):
a['topics'][lens-1]['title'] = list[1][b][1][lens-1]
# a['topics'][1]['title'] = list[1][b][1][1]
b+=1
print(dict)