Q:

recursively traverse multidimensional dictionary and export to csv

I've have a complex multidimensional dictionary that I want to export some of the the key value pairs to a csv file as a running log file. I've tried the various help with exporting to cvs functions and hacked away at most of the code example in stackoverflow on traversing multidimensional dictionaries but have failed to arrive at a solution. This problem is also unique in that it has only some key values I want to export.
Here is the dictionary:
cpu_stats = {'time_stamp': {'hour': 22, 'month': 5, 'second': 43, 'year': 2014, 'day': 29, 'minute': 31}, 'cpus': [[{'metric_type': 'CPU_INDEX', 'value': 1}, {'metric_type': 'CPU_TEMPERATURE', 'value': 39}, {'metric_type': 'CPU_FAN_SPEED', 'value': 12000}]]}

I need to format the values in time_stamp into a yyyy-mm-dd hh:mm:ss and store it as the first cell of the row. I then need the values in 'cpus' for CPU_INDEX, CPU_TEMPERATURE, and CPU_FAN_SPEED in the same row as the time stamp.
The csv file should look like this:
time_stamp, cpu_index, cpu_temperature, cpu_fan_speed
2014-05-29, 1, 38, 12000

One example I've been hacking away on is:
def walk_dict(seq, level=0):
"""Recursively traverse a multidimensional dictionary and print all
keys and values.
"""

items = seq.items()
items.sort()
for v in items:
    if isinstance(v[1], dict):
        # Print the key before make a recursive call
        print "%s%s" % ("  " * level, v[0])
        nextlevel = level + 1
        walk_dict(v[1], nextlevel)
    else:
        print "%s%s %s" % ("  " * level, v[0], v[1])

I get the following output
walk_dict(cpu_stats)

cpus [[{'metric_type': 'CPU_INDEX', 'value': 1}, {'metric_type': 'CPU_TEMPERATURE', 'value': 38}, {'metric_type': 'CPU_FAN_SPEED', 'value': 12000}]]
time_stamp
  day 29
  hour 22
  minute 17
  month 5
  second 19
  year 2014

I have also been hacking away at this function as well hoping I can store the date information into variables that can then be formatted into a single string. Unfortuatly it has recursive calls which loose the local variables on subsequent calls. Using global was futile.
def parseDictionary(obj, nested_level=0, output=sys.stdout):

spacing = '   '
if type(obj) == dict:
    print >> output, '%s{' % ((nested_level) * spacing)
    for k, v in obj.items():
        if hasattr(v, '__iter__'):
            # 1st level, prints time and cpus
            print >> output, '%s:' % (k)
            parseDictionary(v, nested_level + 1, output)
        else:
            # here is the work
            if k == "hour":
                hour = v
            elif k == "month":
                month = v
            elif k == "second":
                second = v
            elif k == "year":
                year = v
            elif k == "day":
                day = v
            elif k == "minute":
                minute = v
            print >> output, '%s %s' % (k, v)
    print >> output, '%s}' % (nested_level * spacing)
elif type(obj) == list:
    print >> output, '%s[' % ((nested_level) * spacing)
    for v in obj:
        if hasattr(v, '__iter__'):
            parseDictionary(v, nested_level + 1, output)
        else:
            print >> output, '%s%s' % ((nested_level + 1) * spacing, v)
    print >> output, '%s]' % ((nested_level) * spacing)
else:
    print >> output, '%s%s' % (nested_level * spacing, obj)

if __name__ == "__main__":
    global year
    global month
    global day
    global hour
    global minute
    global second

    cpu_stats = {'time_stamp': {'hour': 22, 'month': 5, 'second': 43, 'year': 2014, 'day': 29, 'minute': 31}, 'cpus': [[{'metric_type': 'CPU_INDEX', 'value': 1}, {'metric_type': 'CPU_TEMPERATURE', 'value': 39}, {'metric_type': 'CPU_FAN_SPEED', 'value': 12000}]]}
    parseDictionary(cpu_stats)
    print '%s-%s-%s %s:%s:%s' % (year, month, day, hour, minute, second)

output:
{
time_stamp:
   {
hour 22
month 5
second 27
year 2014
day 29
minute 57
cpus:
   [
      [
         {
metric_type CPU_INDEX
value 1
         {
metric_type CPU_TEMPERATURE
value 39
         {
metric_type CPU_FAN_SPEED
value 12000
      ]
   ]
Traceback (most recent call last):
  File "./cpu.py", line 135, in <module>
    print '%s-%s-%s %s:%s:%s' % (year, month, day, hour, minute, second)
NameError: global name 'year' is not defined

Thanks, I appreciate any help in pointing me in the right direction as I'm currently at a loss.

A:

I agree with @desired login, however assuming you have no control of the incoming data and had to work with what you showed in your questions... You could just traverse it like so: 
cpu_stats = {'time_stamp': {'hour': 22, 'month': 5, 'second': 43, 'year': 2014, 'day': 29, 'minute': 31}, 
             'cpus': [ [{'metric_type': 'CPU_INDEX', 'value': 1}, {'metric_type': 'CPU_TEMPERATURE', 'value': 39}, {'metric_type': 'CPU_FAN_SPEED', 'value': 12000} ] ] 
            }

timestamp = ''
for stats in cpu_stats.keys():
    if stats == 'time_stamp':
        timestamp = '{year}-{month}-{day}'.format(**cpu_stats[stats])
    if stats == 'cpus':
        for cpu in cpu_stats[stats]:
            cpu_index = ''
            cpu_temperature = ''
            cpu_fan_speed = ''
            for metric in cpu:
                if metric['metric_type'] == 'CPU_INDEX':
                    cpu_index = str(metric['value'])
                elif metric['metric_type'] == 'CPU_TEMPERATURE':
                    cpu_temperature = str(metric['value'])
                elif metric['metric_type'] == 'CPU_FAN_SPEED':
                    cpu_fan_speed = str(metric['value'])
            print ','.join([timestamp, cpu_index, cpu_temperature, cpu_fan_speed])

