使用csv.DictWriter将数据写入CSV

时间:2017-12-15 23:13:39

标签: python csv

我有一个python字典对象,我需要将数据写入CSV文件。我是python编程的新手。所以我想知道是否有人可以指导我。

这是我的目标。

dict = [{u'interface': [{u'interface-down-reason': u'LACP Convergence Timeout',
                  u'interface-name': u'ethernet28:1',
                  u'leaf-group': u'R2',
                  u'member-type': u'',
                  u'mode': u'lacp',
                  u'op-state': u'down',
                  u'phy-state': u'up',
                  u'switch-name': u'R2L2'},
                 {u'interface-down-reason': u'LACP Protocol Initiated',
                  u'interface-name': u'ethernet28:1',
                  u'leaf-group': u'R2',
                  u'member-type': u'',
                  u'mode': u'lacp',
                  u'op-state': u'down',
                  u'phy-state': u'up',
                  u'switch-name': u'R2L1'}],
  u'name': u'LACP-Test'},
 {u'interface': [{u'interface-down-reason': u'None',
                  u'interface-name': u'ethernet54:4',
                  u'leaf-group': u'R1',
                  u'member-type': u'',
                  u'mode': u'static-auto-vswitch-inband',
                  u'op-state': u'up',
                  u'phy-state': u'up',
                  u'switch-name': u'R1L1'},
                 {u'interface-down-reason': u'None',
                  u'interface-name': u'ethernet54:4',
                  u'leaf-group': u'R1',
                  u'member-type': u'',
                  u'mode': u'static-auto-vswitch-inband',
                  u'op-state': u'up',
                  u'phy-state': u'up',
                  u'switch-name': u'R1L2'}],
  u'name': u'LAX-K8-MASTER-NODE'}]

如您所见,它由多个键值对组成,而某些键具有词典列表。

我一直在阅读csv.Dictwiter,我想要包含字段名称,如下所示

export_fields = ['name','interface-name', 'op-state', 'phy-state']

然而,挑战是一些字段名称在密钥'界面的字典中。

那么我该如何隔离它以便将其写入CSV文件。

如果有人可以分享逻辑或指导我,我可以从那里接受它。

感谢您的回复。

1 个答案:

答案 0 :(得分:1)

如果不添加某种转换层,就不能使用嵌套结构来编写2D表数据 - Python不知道在哪里查找元素,也不知道嵌套如何影响它们。因此,提供的数据结构如下:

data = [{u'interface': [{u'interface-down-reason': u'LACP Convergence Timeout',
                         u'interface-name': u'ethernet28:1',
                         u'leaf-group': u'R2',
                         u'member-type': u'',
                         u'mode': u'lacp',
                         u'op-state': u'down',
                         u'phy-state': u'up',
                         u'switch-name': u'R2L2'},
                        {u'interface-down-reason': u'LACP Protocol Initiated',
                         u'interface-name': u'ethernet28:1',
                         u'leaf-group': u'R2',
                         u'member-type': u'',
                         u'mode': u'lacp',
                         u'op-state': u'down',
                         u'phy-state': u'up',
                         u'switch-name': u'R2L1'}],
         u'name': u'LACP-Test'},
        {u'interface': [{u'interface-down-reason': u'None',
                         u'interface-name': u'ethernet54:4',
                         u'leaf-group': u'R1',
                         u'member-type': u'',
                         u'mode': u'static-auto-vswitch-inband',
                         u'op-state': u'up',
                         u'phy-state': u'up',
                         u'switch-name': u'R1L1'},
                        {u'interface-down-reason': u'None',
                         u'interface-name': u'ethernet54:4',
                         u'leaf-group': u'R1',
                         u'member-type': u'',
                         u'mode': u'static-auto-vswitch-inband',
                         u'op-state': u'up',
                         u'phy-state': u'up',
                         u'switch-name': u'R1L2'}],
         u'name': u'LAX-K8-MASTER-NODE'}]

你必须手动指示它写什么,这在你的情况下相当简单:

# on Python 3.x use: open("output.csv", "wt", endline="")  
with open("output.csv", "wb") as f:  # open output.csv for writing
    writer = csv.writer(f)  # create a CSV writer
    writer.writerow(['name', 'interface-name', 'op-state', 'phy-state'])  # write the header
    for endpoint in data:  # loop through your data
        name = endpoint["name"]
        for it in endpoint["interface"]:  # loop through all interfaces
            writer.writerow([name, it["interface-name"], it["op-state"], it["phy-state"]])