将defaultdict(list)写入文件

问题描述 投票:0回答:2

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

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

         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 python-2.7 defaultdict
2个回答
3
投票

我会这样做(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])

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

现在看起来像这样:

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

(稍微移动,因为制表符不够宽,但这不是读回的问题)

Python 3 用户必须改变:

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
的平面生成器理解,执行速度更快:

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

0
投票

请在下面找到我的答案:

#Writing an instance of  Defaultdict(list) into a file is simpler than I 
# thought. Polymorphisms can solve this problem. 
# Create an object (dd) of type Defaultdict(list) (mydd) which is a subtype of
# dict, Assign mydd to dd and save dd into a file as json.

from collections import defaultdict
import json
dd = defaultdict(list)
dd["horse"].append("Mammal")
dd["Eagle"].append("Bird")
dd["Python"].append("Reptile")
print(dd)

# Let save this defaultdict instance in a file
mydd = dict
mydd = dd
print("#" * 70)
print(f"printing mydd ----:{mydd} ---- an instance of a dict")
# Now saving dd under mydd, an instance of a dict.
print("#" * 70)
with open("mydd.json", "w") as f:
    print("writing my defaultdict object into a file")
    f.write(json.dumps(mydd))
    f.close()
print("#" * 70)
    
mydd1 = dict
# opening the file and store it into an object from the file

with open("mydd.json", "r") as f:
    print("opening my defaultdict object into a file")
    mydd1 = json.load(f)
    print("printing the json file just saved above", mydd1)
© www.soinside.com 2019 - 2024. All rights reserved.