如何在Python中编写字典列表文件

时间:2019-05-20 10:40:57

标签: python

我有以下几组字典:

myListOfDict = {
'READ_NAME': 'NB501050:52:HHMJJBGXY:4:13609:11402:8330', 'CLASS_ID': 3, 'SEQ_ID': 0, 'READ_LEN': 136, 'QVALUES': 'KEOLFC.I?EA;CHEJ?/0CGDBI1GF4D+>-CEB8/J/EFF=HK/0GGGHCFG;GFBH0JG8GG..DGH/BKD?,BHFCFHKF=BH-HFG3BHHGGHFHIBGHFHKDIIJCFGHABGIEHCFFHFHEHEEGQOOO', 'SEQUENCE': 'CACAGGTCTATCACCCTATTAACCCCTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATT', 'MAPPING_POS': 3, 'DELTA': 0, 'ECIGAR_STRING': '24=C111=', 'ECIGAR_LEN': 8, 'REVERSE_COMP': 1, 'MAPPING_SCORE': 70.0
}, 
{
'READ_NAME': 'NB501050:47:HHMJVBGXY:2:11311:23488:9633', 'CLASS_ID': 3, 'SEQ_ID': 0, 'READ_LEN': 136, 'QVALUES': 'K@KJ<E12I1H0I@BGGHFIJFHFIKFCIKFGGGGH=EGG@FBHFJGGGGG0HGHDEKDBIBHFCFIKFGFHBHFHDBIJGGHFEIBGHFIKDIIJCFGHFBIKFHCFF5GHFIGDGGEFEHJFI6JEJEEHRKMN', 'SEQUENCE': 'CTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATTCCTGCCTCATCCTAT', 'MAPPING_POS': 18, 'DELTA': 0, 'ECIGAR_STRING': '131=C4=', 'ECIGAR_LEN': 7, 'REVERSE_COMP': 1, 'MAPPING_SCORE': 70.0
}, 
{...}

对于元组的每个元素(即字典),我只需要将与每个键对应的值打印到文件中。以下是我要寻找的内容:

NB501050:52:HHMJJBGXY:4:13609:11402:8330 3 0 136 KEOLFC.I?EA;CHEJ?/0CGDBI1GF4D+>-CEB8/J/EFF=HK/0GGGHCFG;GFBH0JG8GG..DGH/BKD?,BHFCFHKF=BH HFG3BHHGGHFHIBGHFHKDIIJCFGHABGIEHCFFHFHEHEEGQOOO ..
NB501050:47:HHMJVBGXY:2:11311:23488:9633 3 0 136 K@KJ<E12I1H0I@BGGHFIJFHFIKFCIKFGGGGH=EGG@FBHFJGGGGG0HGHDEKDBIBHFCFIKFGFHBHFHDBIJGGHFEIBGHFIKDIIJCFGHFBIKFHCFF5GHFIGDGGEFEHJFI6JEJEEHRKMN ...

我实现了以下代码:

with open("filename_t", "w") as f:
 for val in myListOfDict:
    for val1 in val.items():
        for val2 in val1:
           f.write(str(val2) + "  ")
    f.write("\n")

但我没有得到想要的东西。

READ_NAME  NB501050:52:HHMJJBGXY:4:13609:11402:8330  CLASS_ID  3  SEQ_ID  0 READ_LEN  136  QVALUES  KEOLFC.I?EA;CHEJ?/0CGDBI1GF4D+>-CEB8/J/EFF=HK/0GGGHCFG;GFBH0JG8GG..DGH/BKD?,BHFCFHKF=BH-HFG3BHHGGHFHIBGHFHKDIIJCFGHABGIEHCFFHFHEHEEGQOOO  SEQUENCE  CACAGGTCTATCACCCTATTAACCCCTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATT  MAPPING_POS  3  DELTA  0  ECIGAR_STRING  24=C111=  ECIGAR_LEN  8  REVERSE_COMP  1  MAPPING_SCORE  70.0  
READ_NAME  NB501050:47:HHMJVBGXY:2:11311:23488:9633  CLASS_ID  3  SEQ_ID  0 READ_LEN  136  QVALUES  K@KJ<E12I1H0I@BGGHFIJFHFIKFCIKFGGGGH=EGG@FBHFJGGGGG0HGHDEKDBIBHFCFIKFGFHBHFHDBIJGGHFEIBGHFIKDIIJCFGHFBIKFHCFF5GHFIGDGGEFEHJFI6JEJEEHRKMN  SEQUENCE  CTATTAACCACTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATTCCTGCCTCATCCTAT  MAPPING_POS  18  DELTA  0  ECIGAR_STRING  131=C4=  ECIGAR_LEN  7  REVERSE_COMP  1  MAPPING_SCORE  70.0 

如何避免打印密钥?

3 个答案:

答案 0 :(得分:2)

您正在打印键和值,因为您正在使用返回键值元组的dict.items()函数。只需将代码更改为:

with open("filename_t", "w") as f:
 for val in myListOfDict:
    for val1 in val.values():
        f.write(str(val1) + "  ")
    f.write("\n")

请注意,字典不能保证您键/值/项的顺序。

答案 1 :(得分:2)

您可以使用csv模块。

例如:

import csv

myListOfDict = [{
'READ_NAME': 'NB501050:52:HHMJJBGXY:4:13609:11402:8330', 'CLASS_ID': 3, 'SEQ_ID': 0, 'FLAGS': [0, 0, 0], 'READ_LEN': 136, 'QVALUES': 'KEOLFC.I?EA;CHEJ?/0CGDBI1GF4D+>-CEB8/J/EFF=HK/0GGGHCFG;GFBH0JG8GG..DGH/BKD?,BHFCFHKF=BH-HFG3BHHGGHFHIBGHFHKDIIJCFGHABGIEHCFFHFHEHEEGQOOO', 'SEQUENCE': 'CACAGGTCTATCACCCTATTAACCCCTCACGGGAGCTCTCCATGCATTTGGTATTTTCGTCTGGGGGGTGTGCACGCGATAGCATTGCGAGACGCTGGAGCCGGAGCACCCTATGTCGCAGTATCTGTCTTTGATT', 'MAPPING_POS': 3, 'DELTA': 0, 'ECIGAR_STRING': '24=C111=', 'ECIGAR_LEN': 8, 'REVERSE_COMP': 1, 'MAPPING_SCORE': 70.0
}]

with open("filename_t", "w") as f:
    writer = csv.DictWriter(f, delimiter=" ", fieldnames=['REVERSE_COMP', 'SEQUENCE', 'CLASS_ID', 'SEQ_ID', 'READ_LEN', 'ECIGAR_LEN', 'FLAGS', 'MAPPING_SCORE', 'ECIGAR_STRING', 'MAPPING_POS', 'QVALUES', 'READ_NAME', 'DELTA'])
    for val in myListOfDict:
        writer.writerow(val)

答案 2 :(得分:2)

with open("filename_t", "w") as f:
    for val in myListOfDict:
        for val1 in val.values():
            if isinstance(val1, list):
                v = ' '.join(map(lambda x: str(x), val1))
            else:
                v = str(val1)
            f.write(v + "  ")
        f.write("\n")