Using the CSV module in python, I was experimenting with the DictWriter class to convert dictionaries to rows in a csv. Is there any way to handle nested dictionaries? Specifically, I'm exporting Disqus comments that have a structure like this:
{
u'status': u'approved',
u'forum': {u'id': u'', u'': u'', u'shortname': u'', u'name': u'', u'description': u''},
u'thread': {u'allow_comments': True, u'forum': u'', u'title': u'', u'url': u'', u'created_at': u'', u'id': u'', u'hidden': False, u'identifier': [], u'slug': u''},
u'is_anonymous': False,
u'author': {u'username': u'', u'email_hash': u'', u'display_name': u'', u'has_avatar': True, u'url': u'', u'id': 1, u'avatar': {u'small': u'', u'large': u'', u'medium': u''}, u'email': u''},
u'created_at': u'2009-08-12T10:14',
u'points': 0,
u'message': u"",
u'has_been_moderated': False,
u'ip_address': u'',
u'id': u'',
u'parent_post': None
}
I wanted to specify fields from the author and thread properties and haven't found a way so far. Here's the code:
f = open('export.csv', 'wb')
fieldnames = ('id','status','is_anonymous','created_at','ip_address','points','has_been_moderated','parent_post','thread')
try:
exportWriter = csv.DictWriter(f,
fieldnames,
restval=None,
extrasaction='ignore',
quoting=csv.QUOTE_NONNUMERIC
)
for c in comments:
exportWriter.writerow(c)
finally:
f.close()