Reputation: 75
How can a python dictionary that looks like this:
{
'RCLS1': [(0, 20), (10, 112), (20, 130), (30, 102)],
'RCLS2': [(0, 16),(10, 53),(20, 96), (30, 45)]
}
be converted to a CSV with structure:
RCLS1, 0, 20
RCLS1, 10, 112
.
.
.
RLCS2, 30, 45
I have tried this:
with open(r'E:\data.csv', "wb") as f:
csv.writer(f).writerows((k,) + v for k, v in dct.items())
but this resulted in the following error:
can only concatenate tuple (not "list") to tuple
Upvotes: 4
Views: 682
Reputation: 1752
Based on the sample input/output you provided, you can iterate through the dictionary's key-value pairs, then iterate through the list of tuple values associated with each key, build the associated CSV row string, and write it to the CSV file, like so:
dct = {'RCLS1':[(0, 20), (10, 112), (20, 130), (30, 102)], 'RCLS2': [(0, 16),(10, 53),(20, 96), (30, 45)]}
int_to_str = lambda int_value: str(int_value)
with open(r"data.csv", "w") as csv_file:
for key, values in dct.items():
for tuple_value in values:
csv_row = [key] + list(map(int_to_str, list(tuple_value)))
csv_file.write(", ".join(csv_row) + "\n")
data.csv results:
RCLS2, 0, 16
RCLS2, 10, 53
RCLS2, 20, 96
RCLS2, 30, 45
RCLS1, 0, 20
RCLS1, 10, 112
RCLS1, 20, 130
RCLS1, 30, 102
Upvotes: 1
Reputation: 2020
one alternative answer using pandas
:
>>> import pandas as pd
>>> d={'RCLS1':[(0, 20), (10, 112), (20, 130), (30, 102)], 'RCLS2': [(0, 16),(10, 53),(20, 96), (30, 45)]}
>>> df=pd.DataFrame(d)
RCLS1 RCLS2
0 (0, 20) (0, 16)
1 (10, 112) (10, 53)
2 (20, 130) (20, 96)
3 (30, 102) (30, 45)
[4 rows x 2 columns]
>>> dfs=df.stack().reset_index(level=0)
level_0 0
RCLS1 0 (0, 20)
RCLS2 0 (0, 16)
RCLS1 1 (10, 112)
RCLS2 1 (10, 53)
RCLS1 2 (20, 130)
RCLS2 2 (20, 96)
RCLS1 3 (30, 102)
RCLS2 3 (30, 45)
>>> dfs=dfs[0].apply(pd.Series) # break the tuples in column with "name"=0
0 1
RCLS1 0 20
RCLS2 0 16
RCLS1 10 112
RCLS2 10 53
RCLS1 20 130
RCLS2 20 96
RCLS1 30 102
RCLS2 30 45
>>> dfs.to_csv('fileName.csv')
Upvotes: 1
Reputation: 9257
If what i understood from your question is correct, your are trying to do something like this (without the need to use of csv
module):
a = {'RCLS1':[(0, 20), (10, 112), (20, 130), (30, 102)], 'RCLS2': [(0, 16),(10, 53),(20, 96), (30, 45)]}
with open('E:\data.csv', 'a+') as f:
for k,v in a.items():
f.write("{0}: {1}\n".format(k,", ".join(", ".join(str(j) for j in k) for k in v)))
Output (The date in your file will be similar to this output):
RCLS1: 0, 20, 10, 112, 20, 130, 30, 102
RCLS2: 0, 16, 10, 53, 20, 96, 30, 45
Otherwise, if you want to have the data by pair you can do something like this:
with open('E:\data.csv', 'a+') as f:
for k,v in a.items():
f.write("{0}: {1}\n".format(k, "".join(", ".join(str(k) for k in v))))
Output:
RCLS1: (0, 20), (10, 112), (20, 130), (30, 102)
RCLS2: (0, 16), (10, 53), (20, 96), (30, 45)
Edit:
A quick solution to your new update. You can do something like this:
a = {'RCLS1':[(0, 20), (10, 112), (20, 130), (30, 102)], 'RCLS2': [(0, 16),(10, 53),(20, 96), (30, 45)]}
with open('E:\data.csv', 'a+') as f:
for k,v in a.items():
for j in v:
f.write("{0}: {1}\n".format(k, ", ".join(str(k) for k in j)))
Output:
RCLS2: 0, 16
RCLS2: 10, 53
RCLS2: 20, 96
RCLS2: 30, 45
RCLS1: 0, 20
RCLS1: 10, 112
RCLS1: 20, 130
RCLS1: 30, 102
Upvotes: 2