lax123
lax123

Reputation: 21

Using csv.DictWriter to write data to CSV

I have a python dictionary object that I need to write data to CSV file. I am new to python programming. So I was wondering if someone can guide me through.

Here is my object.

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'}]

As you see it consists of multiple key value pairs and some keys have a list of dictionaries.

I have been reading csv.Dictwiter, I want to include the field names as shown below

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

However the challenge is some field names are within the dictionary of the key 'interface'.

So how do I segregate this so i can write it to the CSV file.

If someone can share the logic or guide me through, I can take it from there.

Appreciate your response.

Upvotes: 0

Views: 129

Answers (1)

zwer
zwer

Reputation: 25799

You cannot use nested structures to write 2D table data without adding some sort of translation layer - Python doesn't know where to look for your elements nor how the nesting affects them. So, provided data structured as:

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'}]

you have to manually instruct it what to write, which is rather simple in your case:

# 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"]])

Upvotes: 1

Related Questions