0

I want easily to change a dictionary like this (with arbitrary depth, or let's say, up to 7 levels of nesting) into a tabular data structure:

{u'Adjustment': False,
 u'DocNumber': u'2',
 u'Id': u'1005',
 u'Line': [{u'Amount': 50.0,
            u'Description': u'Extra Pay for Cellist for 9/27 Session on "Song Name"',
            u'DetailType': u'JournalEntryLineDetail',
            u'Id': u'0',
            u'JournalEntryLineDetail': {u'AccountRef': {u'name': u'Recorded Music:WIP:Studio Musicians',
                                                        u'value': u'99'},
                                        u'Entity': {u'EntityRef': {u'name': u'Some Vendor',
                                                                   u'value': u'92'},
                                                    u'Type': u'Vendor'},
                                        u'PostingType': u'Debit'}},
           {u'Amount': 50.0,
            u'Description': u'Paid Vendor in Cash for amount over $150 check',
            u'DetailType': u'JournalEntryLineDetail',
            u'Id': u'1',
            u'JournalEntryLineDetail': {u'AccountRef': {u'name': u'3000 Capital Account (contr/withdr)',
                                                        u'value': u'7'},
                                        u'Entity': {u'EntityRef': {u'name': 'Some Name',
                                                                   u'value': u'92'},
                                                    u'Type': u'Vendor'},
                                        u'PostingType': u'Credit'}}],
 u'MetaData': {u'CreateTime': u'2007-09-27T00:00:00-07:00',
               u'LastUpdatedTime': u'2007-09-27T00:00:00-07:00'},
 u'PrivateNote': u'General Journal',
 u'SyncToken': u'0',
 u'TxnDate': u'2007-09-27',
 u'domain': u'QBO',
 u'sparse': False}

What's the best way to accomplish this. The headers for a nested item of list whose key is u'Line', I'd want the end-state header to be Line[0] - Amount, and the name of the EntityRef of the first Line's JournalEntryLineDetail's Entity to be Line[0] - JournalEntryLineDetail - Entity - EntityRef - name.

I know this would make the table extremely wide if the depth was more than a couple levels, but let's assume that's what I want.

I'm hoping there's a module that just does this. I checked out tablib but haven't figure out how to make it do that yet.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
HaPsantran
  • 5,581
  • 6
  • 24
  • 39
  • 1
    Let me clarify, you want to *display* dictionary like this, don't you? – sashkello Jan 30 '14 at 02:25
  • possible duplicate of [pretty printing nested dictionaries in Python?](http://stackoverflow.com/questions/3229419/pretty-printing-nested-dictionaries-in-python) – sashkello Jan 30 '14 at 02:25
  • What sort of "tabular data structure"? – martineau Jan 30 '14 at 02:27
  • He wants to take an existing dictionary like above, and change it into a tabular data structure. – RyPeck Jan 30 '14 at 02:27
  • Correct, RyPeck. Imagine that there were 20 dictionaries that were similar to the above. I'd want this function to return 20 lines, each of which represents one dictionary like the one above. I'm trying to flatten them. (Sorry, I should've been more clear. I printed the above using pretty print; that's not what I'm looking to generate though.) – HaPsantran Jan 30 '14 at 05:52

0 Answers0