python - recursively traverse multidimensional dictionary and export to csv -
i've have complex multidimensional dictionary want export of the key value pairs csv file running log file. i've tried various exporting cvs functions , hacked away @ of code example in stackoverflow on traversing multidimensional dictionaries have failed arrive @ solution. problem unique in has key values want export.
here 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 format values in time_stamp yyyy-mm-dd hh:mm:ss , store first cell of row. need values in 'cpus' cpu_index, cpu_temperature, , cpu_fan_speed in same row time stamp.
the csv file should 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 multidimensional dictionary , print keys , values. """ items = seq.items() items.sort() v in items: if isinstance(v[1], dict): # print key before make 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 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 been hacking away @ function hoping can store date information variables can formatted single string. unfortuatly has recursive calls loose local variables on subsequent calls. using global futile.
def parsedictionary(obj, nested_level=0, output=sys.stdout): spacing = ' ' if type(obj) == dict: print >> output, '%s{' % ((nested_level) * spacing) k, v in obj.items(): if hasattr(v, '__iter__'): # 1st level, prints time , cpus print >> output, '%s:' % (k) parsedictionary(v, nested_level + 1, output) else: # here 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) 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' not defined
thanks, appreciate in pointing me in right direction i'm @ loss.
i agree @desired login, assuming have no control of incoming data , had work showed in questions... traverse 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 = '' stats in cpu_stats.keys(): if stats == 'time_stamp': timestamp = '{year}-{month}-{day}'.format(**cpu_stats[stats]) if stats == 'cpus': cpu in cpu_stats[stats]: cpu_index = '' cpu_temperature = '' cpu_fan_speed = '' 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])
Comments
Post a Comment