1

I have a nested dictionary that contains (nested lists). I have gone through several posts in the Stackoverflow (here, here, and here). But, I am not getting an idea, how to solve the issues. The dictionary looks like

{ 'LinReg': { 'First': [ array([ 0.83333333, -0.77777778, -0.6       ,  0.72222222]),
                         array([0.4       , 0.05555556, 0.4       , 0.44444444])],
              'Second': [ array([[5.16666667, 3.28571429, 2.4       , 6.38461538]]),
                              array([[ 4.83333333, 23.        ,  1.26666667,  3.22222222]])],
              'Third': [ array([[ 5.16666667, -3.28571429, -2.4       ,  6.38461538]]),
                                     array([[ 4.83333333, 23.        ,  1.26666667,  3.22222222]])],
              'Fourth': [ array([0.83333333, 0.77777778, 0.6       , 0.72222222]),
                                array([0.4       , 0.05555556, 0.4       , 0.44444444])]},
  'kNN': { 'First': [ array([ 0.        , -0.75      ,  0.5       ,  0.41666667]),
                      array([ 0.        , -0.8       , -0.1       ,  0.08333333])],
           'Second': [ array([[1.        , 2.33333333, 4.        , 7.4       ]]),
                           array([[       inf, 2.75      , 1.4       , 1.41666667]])],
           'Third': [ array([[ 0.        , -2.33333333,  4.        ,  7.4       ]]),
                                  array([[ 1.        , -2.75      , -1.4       ,  1.41666667]])],
           'Fourth': [ array([0.        , 0.75      , 0.5       , 0.41666667]),
                             array([0.        , 0.8       , 0.1       , 0.08333333])]}}

The code I am trying

a = ["Album/Track"] + dictionary.keys()
x = list(set([y for z in dictionary.values() for y in z.keys()]))
rows = [a] + [[q] + [dictionary[p].get(q, "-") for p in a[1:]] for q in x]
with open("my.csv", "wb") as csvfile:
    writer = csv.writer(csvfile)
    for row in rows:
        writer.write(row)

How can I convert this dictionary into a CSV file like the below one?

LinReg First Array1_Output1, Array1_Output2, and so on
             Array2_Output1, Array2_Output2, and so on
       Second 
       Third
       Fourth
kNN First Array1_Output1, Array1_Output2, and so on
             Array2_Output1, Array2_Output2, and so on
       Second 
       Third
       Fourth
Opps_0
  • 408
  • 4
  • 19
  • Can you edit the question and put there expected output? – Andrej Kesely May 21 '21 at 19:09
  • What are you trying to get? – Onyambu May 21 '21 at 19:44
  • It is very difficult to answer your question without seeing any of your data nor any of the code that you have written that produces your problem. Please review Jon Skeet's excellent blog post [WRITING THE PERFECT QUESTION](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) for guidance in updating your question. For a more detailed tutorial on asking a good question see Eric S. Raymond's site [How To Ask Questions The Smart Way](http://www.catb.org/~esr/faqs/smart-questions.html) – itprorh66 May 21 '21 at 19:52
  • What, in words, does the data you posted represent? – norie May 21 '21 at 20:01
  • @AndrejKesely and Onyambu updated the expected output – Opps_0 May 21 '21 at 20:17

2 Answers2

0
a_file = open("sample.csv", "w")
a_dict = {Key0: Value0, Key1: Value1....}

writer = csv.writer(a_file)
for key, value in a_dict.items():
    writer.writerow([key, value])

Does this apporach help you? What should the CSV file look like at the end?

Aru
  • 352
  • 1
  • 11
0

If d is your dictionary from the question, you can do:

import csv

with open("data.csv", "w") as f_in:
    writer = csv.writer(f_in)
    for k, v in d.items():
        for kk, vv in v.items():
            for vvv in vv:
                writer.writerow([k, kk] + list(vvv.ravel()))

Creates data.csv (screenshot from LibreOffice):

enter image description here

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91