Python根据层次结构转置树状数据

时间:2019-11-19 20:07:22

标签: python python-3.x tree transform hierarchy

我正在使用Python 3.6。我的csv数据如下:

id,name,parent_id,level
1,test-name-1,0,1
2,test-name-2,1,2
3,test-name-3,2,3
4,test-name-4,3,4
5,test-name-5,4,5
6,test-name-6,5,6
7,test-name-7,5,6
8,test-name-8,5,6
9,test-name-9,4,5
10,test-name-10,9,6

我想解析数据并将其放入以下格式:

id, name, level1, level2, level3, level4, level5, level6
1, test-name-1, 1, null, null, null, null, null
2, test-name-2, 1, 2, null, null, null, null
3, test-name-3, 1, 2, 3, null, null, null
4, test-name-4, 1, 2, 3, 4, null, null
5, test-name-5, 1, 2, 3, 4, 5, null
6, test-name-6, 1, 2, 3, 4, 5, 6
7, test-name-7, 1, 2, 3, 4, 5, 7 
8, test-name-8, 1, 2, 3, 4, 5, 8
9, test-name-9, 1, 2, 3, 4, 9, null
10, test-name-10, 1, 2, 3, 4, 9, 10 

基本上,test-name-1是根节点,因此它只能具有level1,不能具有其他级别。 test-name-4是4级,因此它具有(基于其父级,它应该具有4级以下的数据);等等。

我试图按如下方式创建层次结构:

import re
import csv
from collections import defaultdict

file = 'path/to/file/filename.txt'
parents = defaultdict(list)
with open(file, 'r') as f:
    reader = csv.reader(f, delimiter=',')
    for line in list(reader):
        #print(line)
        id = line[0]
        name = line[1]
        level = line[3]
        parent_id = line[2]
        parents[parent_id].append((id, name, level))

print(parents)

def print_data(d, val):
    for id, name, level in d[val]:
        print(id, name, level)
        print_data(d, id)

print_data(parents, '0')

输出为(第一次打印,以显示缓存的对象):

defaultdict(<class 'list'>, {'parent_id': [('id', 'name', 'level')], '0': [('1', 'test-name-1', '1')], '1': [('2', 'test-name-2', '2')], '2': [('3', 'test-name-3', '3')], '3': [('4', 'test-name-4', '4')], '4': [('5', 'test-name-5', '5'), ('9', 'test-name-9', '5')], '5': [('6', 'test-name-6', '6'), ('7', 'test-name-7', '6'), ('8', 'test-name-8', '6')], '9': [('10', 'test-name-10', '6')]})

第二张照片:

1 test-name-1 1
2 test-name-2 2
3 test-name-3 3
4 test-name-4 4
5 test-name-5 5
6 test-name-6 6
7 test-name-7 6
8 test-name-8 6
9 test-name-9 5
10 test-name-10 6

基本上,我不能基于级别递归提取数据。有人可以在这里协助吗?

0 个答案:

没有答案
相关问题