用一个键和两个值将字典写到csv文件中

时间:2019-02-07 21:11:02

标签: python pandas csv sorting dictionary

我有一本有一个键和两个值的字典。我想将字典写到csv文件中,并根据值之一进行排序。我还希望每个值在csv文件中都有自己的列。 我似乎做不到。

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])

我知道有人说要尝试 writer.writerow([k] + sorted_combined) 要么 writer.writerow(键,*值)

,但没有一个有效。我收到的错误消息是:无法连接元组。

我期望得到的是以下内容:

 Subject     Sij      gij
 sub001_01   6578     18
 sub992_03   3820     5

*****编辑******* 这就是我的sorted_combined的样子。但是,例如,您最终看到的(1,6)不再是元组,而是字符串。命名字典键时,每个元组都转换为一个字符串。

[('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.7696499999999999999)),('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, )',(2897317.0,30.266200000000001)),('ne twork6_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))

2 个答案:

答案 0 :(得分:1)

我编辑了答案,您没有使用字典。您有一个元组列表。

import csv 
with open('output.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerow(['Subject', 'Sij', 'gij'])
    for row in sorted_combined:
        l = [row[0]]
        l.append(row[0][1])
        l.append(row[1][1])
        writer.writerow(l)

就像您最初尝试使用csv.writer一样

答案 1 :(得分:1)

您可以生成列表,将其展平,然后只需使用CSV即可将其写入。不过,我不知道有没有更短的方法。

import csv

my_dict = {"sub001_01": [6578,18], "sub992_03": [3820,5]}

with open('my_file.csv', 'w') as f:
    w=csv.writer(f)
    w.writerow(["Subject", "Sij", "gij"])
    for i in list(my_dict.items()):
        #We first make a list like: ['test', 1, 2, 3]
        list1=[a for a in i]
        list_final=[]
        list_final.append(i[0])
        for c in list1[1]:
            list_final.append(c)
        #We use writerow() to write our new list
        w.writerow(list_final)

输出:

Subject,Sij,gij
sub001_01,6578,18
sub992_03,3820,5

要使用'\ t'代替逗号,只需指定定界符

w=csv.writer(f,delimiter="\t")

哪个会产生:

Subject Sij gij
sub001_01   6578    18
sub992_03   3820    5

P.S:这是针对Python 3.X

编辑:忘记提及这是一个通用循环,可以处理2个以上的值。您也可以将其与其他字典一起使用(列表包含多个值)。