-2

I have multiple python dictionaries, each having multiple values. I want to write them in csv files.Dictionaries are of the kind:

dict1='198': Counter({'65': 9.561,
                      '92': 7.845,
                      '22': 5.994}),

dict2={'613': {},
       '201': {'5351': ['-'],
                  '7': ['+'],
                 '16': ['+'],
                 '43': ['*'],
                 '41': ['*'],
                 '9026': ['+']}}

dict3={'200': [('*',
                'https://m.instantmess.com//stream.php?story_id=2137751112'),
                ('+', '-'),
                ('*',
                 'https://m.instantmess.com//stream.php?story_id=213655363'),
                ('*',
                 'https://m.instantmess.com//stream.php?story_id=2133332333'),
                )]

Output Format Required:

1842    ('*', 'https://m.instantmess.com//stream.php?id=1443556433')  88  + 0.97

The first value from dict3 will act as a primary key.
How to add them in a single csv file. I have tried these answers:
Write Python dictionary with multiple values to CSV
Writing multiple Python dictionaries to csv file

But my case is of multiple dictionaries with multiple values. How to solve it?

martineau
  • 119,623
  • 25
  • 170
  • 301
Beginner
  • 721
  • 11
  • 27
  • Can you give an example of your expected output? – wholevinski Sep 12 '18 at 16:51
  • 1
    Your `dict2` has a missing curly brace. – Adrian Keister Sep 12 '18 at 16:52
  • Also what is `Counter`? – Adrian Keister Sep 12 '18 at 16:53
  • I am just pasting all the values from the console ,counter is counter dictionary from collections library, missing brace must be a unintentionally left – Beginner Sep 12 '18 at 16:54
  • 1
    Also, just as a formatting matter: if you indent code with four spaces, you no longer need the ` character. It just messes things up. – Adrian Keister Sep 12 '18 at 16:56
  • As `88` and `0.97` do not appear in any of your source data and you don't *describe* what the output is supposed to contain, I have no idea what output you're looking for. If you also need help with some kind of computation or aggregation, your question is Too Broad. Why do you want it in CSV format, anyway? JSON would be a much more natural fit for nested `dict`s and `list`s. – jpmc26 Sep 12 '18 at 23:41

1 Answers1

3

You could try making a list of the dictionaries, then making a pandas DataFrame out of the list, then using the .to_csv() method of the DataFrame. Something like this:

import pandas as pd
my_list = [dict1, dict2, dict3]
df = pd.DataFrame(my_list)
df.to_csv('path to file')
Adrian Keister
  • 842
  • 3
  • 15
  • 33
  • I tried it but its not coming in the format I need, its just coming as a big list.Let me add the output format ,I want – Beginner Sep 12 '18 at 16:49