Reputation: 103
I've been trying to save my dictionary to a csv file. So I want it to look something like this:
Subject value1 concussion
network3_UCA_0073_01_(0, 1) 0.57 no
network3_UCA_0073_02_(0, 1) 0.64 no
network3_UCA_0073_03_(0, 1) 0.59 no
network3_UCA_0075_01_(0, 1) 0.69 no
The dictionary looks like this:
{}
{}
{}
{}
{}
{}
{'network3_UCA_0073_01_(0, 1)': [(0.57, 'no')]}
{'network3_UCA_0073_02_(0, 1)': [(0.64, 'no')]}
{'network3_UCA_0073_03_(0, 1)': [(0.59, 'no')]}
{}
{}
{'network3_UCA_0075_01_(0, 1)': [(0.69, 'no')]}
{'network3_UCA_0075_02_(0, 1)': [(0.62, 'no')]}
{'network3_UCA_0075_03_(0, 1)': [(0.57, 'no')]}
{'network3_UCA_0076_01_(0, 1)': [(0.38, 'no')]}
{'network3_UCA_0076_02_(0, 1)': [(0.60, 'no')]}
{'network3_UCA_0076_03_(0, 1)': [(0.68, 'no')]}
{'network3_UCA_0077_01_(0, 1)': [(0.64, 'no')]}
{'network3_UCA_0077_02_(0, 1)': [(0.58, 'no')]}
{'network3_UCA_0077_03_(0, 1)': [(0.48, 'no')]}
The code I've been using to achieve this looks like the following (but unfortunately it hasn't been working):
with open(dirforR , 'a') as f:
writer = csv.writer(f)
writer.writerow(['Subject', 'value1', 'concussion'])
for row in sorted_combined:
#print row[0]
l = [row[0]]
l.append(row[1][0])
l.append(row[1][1])
writer.writerow(l)
I'm not sure if its because the dictionary's key's are separated by \n , but any help would be greatly appreciated. I've tried to figure it out with no hope.
Thanks for your help. I hope to hear back soon . *********EDIT***********
I realized that this is no ordinary dictionary, I actually have it set to:
sorted_combined = defaultdict(list)
the reason for this is because I was merging two dictionaries together and this was the only way I was able to go about it.. Now I'm not quite sure how to move my dictionary list to a csv file.
Sorry this got a little messy.
***EDIT2 ***this is the code that was used to make my defaultdict list called: network_combined
network3 = defaultdict(list)
combined_MDD = {key : (MDD_network3[key], MDD_network3_yesno[key]) for key
in MDD_network3}
combined_HC = {key : (HC_network3[key], HC_network3_yesno[key]) for key in HC_network3}
for k, v in chain(combined_MDD.items(), combined_HC.items()):
network3[k].append(v)
sorted_combined = {k:v for k,v in network3.items() if k.endswith('(0, 1)')}
Upvotes: 5
Views: 4643
Reputation: 919
With some editing I think i was able to work with your example
with open(dirforR , 'a') as f:
writer = csv.writer(f)
writer.writerow(['Subject', 'value1', 'concussion'])
for row in sorted_combined:
if len(row)==0:
pass
else:
l=[val[0] for val in row.items()]
for value in list([val[0] for val in row.values()][0]):
l.append(value)
writer.writerow(l)
hope it helps
Upvotes: 1
Reputation: 3816
You can use csv's dictionary writer for this, simply do a:
import csv
csv.DictWriter(open("path/to/writefile.csv","wb"),fieldnames=dict_to_write[dict_to_write.keys()[0]].keys(),delimiter=","")
alternatively you can use pandas as a more easier option:
import pandas as pd
pd.DataFrame(dict_to_write).to_csv("file.csv")
for you particular use case, assuming the sample you have given is in a file:
import pandas as pd
with open("dictionary.txt","r") as file:
raw_d=file.read().splitlines()
raw_d=[eval(x) for x in raw_d if len(x)>2]
pd.DataFrame([(k,v[0][0],v[0][1]) for x in raw_d for k,v in x.items()]).to_csv("converted_dict.csv")
Upvotes: 3