将字典关联到.txt文件

时间:2014-01-22 17:18:26

标签: python dictionary associations output blast

我在file1中创建了一个重叠群及其长度的字典。 我也有file2,它是表格格式的爆炸输出,它包含重叠群对齐(但不是全部)和一些其他信息,如匹配开始和结束等。 为了计算查询和主题覆盖率,我需要将file1中的长度与file2中的长度相关联。 怎么做? 感谢

2 个答案:

答案 0 :(得分:1)

假设file1为:

contig1 134
contig2 354
contig3 345

您的脚本看起来像

import re

contigDict={}
with open('file1') as c1:
    text=c1.readlines()
for line in text:
    key,value = line.split()
    contigDict[key]=value
with open('file2') as c2:
    scrambled_text=c2.read()

contigs = re.findall(r'contig\d+',scrambled_text)
output = {}
for contig in contigs:
    output[contig]=contigDict[contig]
with open('file3',w) as w:
    for key in output.keys():
        w.write(key+'\t'+output[key]+'\n')

答案 1 :(得分:0)

这是有效的

import re
r=open('result.txt','w')
subjectDict={}
with open('file1.txt') as c1:
    text=c1.readlines()
for line in text:
    key,value = line.split()
    subjectDict[key]=value
with open('file2.txt') as c2:
    lines=c2.readlines()
for line in lines:
    new_list=re.split(r'\t+',line)
    s_name=new_list[0]
    subjects = re.findall(r'contig\d+',s_name)
    output = {}
    for subject in subjects:
        output[subject]=subjectDict[subject]
        r.writelines(subjectDict[subject]+'\n')