tags:

views:

270

answers:

1

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()
+1  A: 

I think the main problem your going to have is how to represent a nested data structure in one flat row of csv data.

You could use some form of name mangeling to flatten the keys from the sub dict's into the top level dict.

eg thread': {u'allow_comments':

would become thread_allows_comments.

Mark
I'd like to just pick a few out, not necessarily convert the sub-dictionary completely, so I was hoping there was just a way to address the sub dictionary properties. But, you're right, probably have to pre-process and flatten the dictionaries first.
RyanW