I have a dictionary in the format:
cu = {'m':[[a1,a2],[a3,a4],[a5,a6]], 'n':[[b1,b2], [b3,b4]]}
#the code I used to save the dictionary in csv file was:
#using numpy to make the csv file
import numpy as np
# using the savetxt
np.savetxt("cu_ck.csv", cu , delimiter ="," , fmt ='% s')
and it raised an error stating that: ValueError: Expected 1D or 2D array, got 0D array instead
Please help me write a code which can be used to save a dictionary of such type. And it is to be noted that, this dictionary is only for example basis...the original dictionary has keys more than 12, wherein the length of values for each key may vary but are in the same format as stated in the cu dictionary.
The csv file should at least look like this:
m a1 a2
m a3 a4
m a5 a6
n b1 b2
n b3 b4
CodePudding user response:
The error is caused by cu
being a dictionary type, which is not an array type.
However, simply converting to an array isn't going to work either, since what you want is fairly complicated. One way to perform this data transformation is to append the key to each subarray:
([['m', a1, a2], ['m', a3, a4], ['m', a5, a6]], [['n', b1, b2], ['n', b3, b4]])
and then concatenate the outer lists:
[['m', a1, a2], ['m', a3, a4], ['m', a5, a6], ['n', b1, b2], ['n', b3, b4]]
Admittedly, I don't know whether this is very Pythonic, but it does the trick:
cu_arr2d = sum(([[key, *row] for row in cu[key]] for key in cu), [])
Here the
([[key, *row] for row in cu[key]] for key in cu)
is iterating over all the keys and then iterating over all the rows of that key, and appending the key to the row. Its output is that tuple of 2d lists from the top of this post. Then the sum
is concatenating everything.
CodePudding user response:
Assuming that a1
and similar are numpy.array()
, then you might use a list comprehension to dive into the nested dictionary are write the results via:
import csv
import numpy
cu = {
'm': [
[numpy.array([0,0,0]),numpy.array([1,1,1]),numpy.array([2,2,2])],
[numpy.array([3,3,3]),numpy.array([4,4,4])],
[numpy.array([5,5,5]),numpy.array([6,6,6])],
],
'n': [
[numpy.array([1,1,1]),numpy.array([2,2,2])],
[numpy.array([3,3,3]),numpy.array([4,4,4])]
]
}
with open("out.csv", "w", newline="") as file_out:
writer = csv.writer(file_out, quoting=csv.QUOTE_ALL)
writer.writerows(
[key] list(map(list, inner_value))
for key, outer_value in cu.items()
for inner_value in outer_value
)
This will give you a file with contents like:
"m","[0, 0, 0]","[1, 1, 1]","[2, 2, 2]"
"m","[3, 3, 3]","[4, 4, 4]"
"m","[5, 5, 5]","[6, 6, 6]"
"n","[1, 1, 1]","[2, 2, 2]"
"n","[3, 3, 3]","[4, 4, 4]"
If a1
is just a string then the answer can be slightly simplified to change:
[key] list(map(list, inner_value))
to
[key] inner_value