Linked Questions

Popular Questions

I have a dictionary that has one key and two values. I want to write a dictionary to a csv file, and sorted according to one of the values. I also want each value to have its own column in the csv file. I can't seem to do it.

sorted_combined = sorted(combined.items(), key = lambda kv: kv[1][1])
with open('output.csv', 'wb') as output:
   writer = csv.writer(output)
   writer.writerow(["Subject", "Sij", "gij"])
for key, value in sorted_combined.iteritems():
   writer.writerow(k, sorted_combined[k])

I know some people have said to try writer.writerow([k] + sorted_combined) or writer.writerow(key, *value)

and neither one works. The error messages I get are: cannot concatenate tuple.

What I expect to get is the following:

 Subject     Sij      gij
 sub001_01   6578     18
 sub992_03   3820     5

*****EDIT******* This is what my sorted_combined looks like.. however the (1,6) for instance you see at the end it not a tuple anymore it's a string. When naming the dictionary key, each tuple was converted to a string.

[('network6_QNS_0045_01_(1, 6)', (0.0, 0.0)), ('network6_QNS_0045_01_(1, 4)', (0.0, 0.0)), ('network6_QNS_0045_01_(0, 6)', (0.0, 0.0)), ('network6_QNS_0045_01_(2, 5)', (0.0, 0.0)), ('network6_QNS_0045_01_(1, 7)', (0.0, 0.0)), ('network6_QNS_0045_01_(1, 5)', (0.0, 0.0)), ('network6_QNS_0045_01_(1, 3)', (0.0, 0.0)), ('network6_QNS_0045_01_(5, 6)', (0.0, 0.0)), ('network6_QNS_0045_01_(3, 5)', (0.0, 0.0)), ('network6_QNS_0045_01_(2, 6)', (743466.0, 18.387329999999999)), ('network6_QNS_0045_01_(5, 7)', (142774.0, 18.769649999999999)), ('network6_QNS_0045_01_(0, 5)', (232822.0, 20.160640000000001)), ('network6_QNS_0045_01_(3, 6)', (780163.0, 24.748139999999999)), ('network6_QNS_0045_01_(2, 3)', (199652.0, 26.635860000000001)), ('network6_QNS_0045_01_(4, 7)', (2248433.0, 27.278729999999999)), ('network6_QNS_0045_01_(3, 4)', (922289.0, 27.979320000000001)), ('network6_QNS_0045_01_(1, 2)', (396823.0, 29.924759999999999)), ('network6_QNS_0045_01_(4, 6)', (2897317.0, 30.266200000000001)), ('network6_QNS_0045_01_(0, 4)', (520923.0, 31.040569999999999)), ('network6_QNS_0045_01_(4, 5)', (6358.0, 32.68)), ('network6_QNS_0045_01_(2, 4)', (3622715.0, 35.321170000000002)), ('network6_QNS_0045_01_(2, 7)', (364815.0, 37.499250000000004)), ('network6_QNS_0045_01_(0, 1)', (145240.0, 38.878059999999998)), ('network6_QNS_0045_01_(0, 7)', (224456.0, 46.5182)), ('network6_QNS_0045_01_(0, 3)', (1692.0, 56.884950000000003)), ('network6_QNS_0045_01_(6, 7)', (280955.0, 57.616190000000003)), ('network6_QNS_0045_01_(3, 7)', (2012.0, 71.302719999999994)), ('network6_QNS_0045_01_(0, 2)', (1660.0, 84.085009999999997))]

Related Questions