title: Transform nested Python dictionary to get same-level key values on the same row in CSV output
body: <p>The following code represents a sample a log data I'm trying to transform and export to CSV. It can either have a nested dict for <em>warning</em> and <em>error</em> (ex: agent 1) or have no dict for <em>warning</em> or <em>error</em> (ex: agent 2). </p>
  
  <pre><code>dlog = \
  [
      {
          'agentName': 'agent 1',
          'date': '2018-03-26',
          'fileName': 'log_2018-3-26.log',
          'warning':
              {
                  'Street': 618,
                  'Suite': 470,
                  'TargetID': 558,
                  'Error loading page frame': 27,
                  'writeOverride': 51,
                  'State': 53,
                  'Zip': 52,
                  'PhoneNumber': 5
              },
          'error':
              {
                  'Locations error handling': 1,
                  'The web browsers are not responding': 13
              }
      },
      {
          'agentName': 'agent 2',
          'date': '2018-03-26',
          'fileName': 'log_2018-3-26.log'
      }
  ]
  
  with open('C:\\users\\dbrewer\\documents\\LastLogTest.csv', 'w', newline='') 
  as csvOutput:
      fieldnames = ['agentName', 'date', 'fileName', 'warnings', 'warningCount', 'errors', 'errorCount']
      dict_writer = csv.DictWriter(csvOutput, fieldnames)
      dict_writer.writeheader()
      for d in dlog:
          dict_writer.writerow(d)
  </code></pre>
  
  <p><strong>This is the output I'm getting:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/6NsS6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6NsS6.png" alt="Wrong"></a></p>
  
  <p>As you can see, I'm not breaking down the nested dict for warning and error the way I want too. I've been able to produce a dataset that turns the nested dict values into columns.</p>
  
  <p><strong>This is partially correct</strong></p>
  
  <p><a href="https://i.stack.imgur.com/7g9QC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7g9QC.png" alt="Half Correct"></a></p>
  
  <p>However,this will not work for the working data because there are dozens of different <em>warning</em> and <em>error</em> keys, some of considerable length. What I need is to export the related key, value pairs (agentName, date, fileName) for each key, value pair in the nested warning and error dicts. This would produce 9 rows and 7 columns for the dataset (below)</p>
  
  <p><strong>This is the output I need:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/x5gov.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x5gov.png" alt="Correct"></a></p>
  
  <p>Obviously there is a solution for this, but all I've been able to find here is examples transforming the nested keys into columns (not rows). What's the best approach for tacking this type of transformation?</p>