简体   繁体   English

如何从csv创建分层字典?

[英]How to create a hierarchical dictionary from a csv?

I am trying to build a hierarchical dict (please see below the desired output I am looking for) from my csv file. 我正在尝试从我的csv文件构建一个分层的dict(请在下面寻找我想要的输出)。

The following is my code so far, I was searching through itertools possibly I think that's the best tool I need for this task. 以下是我到目前为止的代码,我正在搜索itertools可能我认为这是完成此任务所需的最佳工具。 I cannot use pandas . 我不能用pandas I think I need to maybe put the values of the key into a new dictionary and then try to map the policy interfaces and build a new dict ? 我想我可能需要将密钥的值放入新字典中,然后尝试映射策略接口并建立新的dict

import csv
import pprint
from itertools import groupby

new_dict=[]
with open("test_.csv", "rb") as file_data:
    reader = csv.DictReader(file_data)

    for keys, grouping in groupby(reader, lambda x: x['groupA_policy']):
        new_dict.append(list(grouping))

pprint.pprint(new_dict)

My csv file looks like this: 我的csv文件如下所示:

GroupA_Host,groupA_policy,groupA_policy_interface,GroupB_Host,GroupB_policy,GroupB_policy_interface
host1,policy10,eth0,host_R,policy90,eth9
host1,policy10,eth0.1,host_R,policy90,eth9.1
host2,policy20,eth2,host_Q,policy80,eth8
host2,policy20,eth2.1,host_Q,policy80,eth8.1

The desired output I want achieve is this: 我想要实现的期望输出是这样的:

[{'GroupA_Host': 'host1',
  'GroupB_Host': 'host_R',
  'GroupB_policy': 'policy90',
  'groupA_policy': 'policy10',
  'interfaces': [{'GroupB_policy_interface': 'eth9',
                  'group_a_policy_interfaces': 'eth0'},
                 {'GroupB_policy_interface': 'eth9.1',
                  'group_a_policy_interface': 'eth0.1'}]},
 {'GroupA_host': 'host2',
  'GroupB_Host': 'host_Q',
  'GroupB_policy': 'policy80',
  'groupA_policy': 'policy20',
  'interfaces': [{'GroupB_policy_interface': 'eth8',
                  'groupA_policy_interfaces': 'eth2'},
                 {'groupA_policy_interface': 'eth8.1',
                  'groupA_policy_interfaces': 'eth2.1'}]}]

I don't think itertools is necessary here. 我认为itertools在这里没有必要。 The important thing is to recognize that you're using ('GroupA_Host', 'GroupB_Host', 'groupA_policy', 'GroupB_policy') as the key for the grouping -- so you can use a dictionary to collect interfaces keyed on this key: 重要的是要认识到您正在使用('GroupA_Host', 'GroupB_Host', 'groupA_policy', 'GroupB_policy')作为分组的键-因此,您可以使用字典来收集此键上键入的接口:

d = {}

for row in reader:
    key = row['GroupA_Host'], row['GroupB_Host'], row['groupA_policy'], row['GroupB_policy']
    interface = {'groupA_policy_interface': row['groupA_policy_interface'], 
                 'GroupB_policy_interface': row['GroupB_policy_interface']
    }

    if key in d:
        d[key].append(interface)
    else:
        d[key] = [interface]

as_list = []
for key, interfaces in d.iteritems():
    record = {}
    record['GroupA_Host'] = key[0]
    record['GroupB_Host'] = key[1]
    record['groupA_policy'] = key[2]
    record['GroupB_policy'] = key[3]
    record['interfaces'] = interfaces
    as_list.append(record)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM