将 defaultdict(list) 写入文件 [英] Writing defaultdict(list) to file

查看:58
本文介绍了将 defaultdict(list) 写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前问过一个问题使用defaultdict来解析多分隔符文件

虽然我确实根据代码获得了所需的输出,但我正在努力以这种形式将其作为表格写入文件

While I do get the desired output based on the code, I am struggling to write it to a file as a table in this form

         count pos _pos _neg
31022550     
31022550    
31022550    
31022550

ID:

for key, rows in ids.iteritems():
     for row in rows:
         print '{}\t{}'.format(key, row)

31022550    {'count': '0', 'base': '=', 'pos': '20', '_neg': '0', '_pos': '0'}
31022550    {'count': '2', 'base': 'A', 'pos': '20', '_neg': '0', '_pos': '2'}
31022550    {'count': '0', 'base': 'C', 'pos': '20', '_neg': '0', '_pos': '0'}
31022550    {'count': '1391', 'base': 'G', 'pos': '20', '_neg': '672', '_pos': '719'}
31022550    {'count': '1', 'base': 'T', 'pos': '20', '_neg': '1', '_pos': '0'}
31022440    {'count': '0', 'base': 'N', 'pos': '20', '_neg': '0', '_pos': '0'}
31022550    {'count': '2', 'base': '+A', 'pos': '20', '_neg': '0', '_pos': '2'}
31022551    {'count': '0', 'base': '=', 'pos': '20', '_neg': '0', '_pos': '0'}
31022551    {'count': '960', 'base': 'A', 'pos': '20', '_neg': '464', '_pos': '496'}
31022551    {'count': '0', 'base': 'C', 'pos': '20', '_neg': '0', '_pos': '0'}
31022551    {'count': '13', 'base': 'G', 'pos': '20', '_neg': '9', '_pos': '4'}
31022551    {'count': '0', 'base': 'T', 'pos': '20', '_neg': '0', '_pos': '0'}
31022551    {'count': '0', 'base': 'N', 'pos': '20', '_neg': '0', '_pos': '0'}
31022551    {'count': '288', 'base': '+G', 'pos': '20', '_neg': '117', '_pos': '171'}
31022551    {'count': '9', 'base': '+GG', 'pos': '20', '_neg': '4', '_pos': '5'}
31022551    {'count': '1', 'base': '+GGG', 'pos': '20', '_neg': '0', '_pos': '1'}

代码

with open('mycsvfile.csv', 'w') as f:
    writer = csv.writer(f)
    for k, v in ids.iteritems():
        writer.writerow([k] + v)

推荐答案

我会这样做(python 2):

I would do this (python 2):

with open('mycsvfile.csv', 'wb') as f:  # binary is better, avoids blank lines in some python 2 versions
    writer = csv.writer(f,delimiter="\t")
    keys=["count","pos","_pos","_neg"]
    writer.writerow([""]+keys)
    for k, vl in ids.iteritems():
        for v in vl:
            writer.writerow([k] + [v[key] for key in keys])

您需要一个双循环来迭代每个键的列表.我已经将列名存储在一个列表中,所以我可以重用它来构建列表理解中的行 &标题也是一样(第一项没有标题,我只是将其留空)

you need a double loop to iterate on the lists for each key. I have stored the column names in a list, so I can reuse it to build the rows in a list comprehension & for the title as well (first item doesn't have a title, I just left it blank)

现在看起来像这样:

        count   pos     _pos    _neg
31022550        0       20      0       0
31022550        2       20      2       0
31022550        0       20      0       0

(略有偏移,因为制表符不够宽,但读回来不是问题)

(slightly shifted because tab character isn't wide enough, but not an issue to read it back)

Python 3 用户必须改变:

Python 3 users would have to change:

with open('mycsvfile.csv', 'wb') as f:

with open('mycsvfile.csv', 'w',newline="") as f:

for k, vl in ids.iteritems():

for k, vl in ids.items():  # also works in python 2

请注意,writerow 双循环可以替换为单行,双循环、平面生成器理解传递给 writerows,执行速度更快:

note that the writerow double loop could be replaced by a single line, a double-loop, flat generator comprehension passed to writerows, faster to execute:

writer.writerows([k] + [v[key] for key in keys] for k, vl in ids.items() for v in vl)

这篇关于将 defaultdict(list) 写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆