如何组合来自不同文档的不同数据?

2024-06-06 06:07:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试合并来自不同文件的数据,以便对于每个项目,一个文档中的OTU编号及其指定的物种可以与另一个文档中的OTU序列相结合,其中OTU编号在一行中,序列在下一行中

我已经设法从第一个文档中添加了所需的数据,但是我根本无法从第二个文档中为同一个OTU添加任何内容

有人有什么建议吗

tax_file=open("C:\\Users\\Line\\Documents\\regnorme\\Bioinformatic\\New_folder1\\tax_new\\16s_tree\\16s_tree5\\taxonomy_tree5.txt")

fasta_file=open("C:\\Users\\Line\\Documents\\regnorme\\Bioinformatic\\New_folder1\\tax_new\\16s_tree\\16s_tree5\\fasta_tree5.2.txt")

OTUs_in_shared=('Otu0001', ... other otus here ...., 'Otu2381')

Vermi_OTUs = []
for line in tax_file:
if line.startswith("Otu"):
    if line.split()[0] in OTUs_in_shared:
        if line.split()[7].startswith("Verminephrobacter"):
            OTU={'OTUnr':(line.split()[0]), 'Family':(line.split()[6]), 
'Genera':(line.split()[7])}
            Vermi_OTUs.append(OTU)

for line in Vermi_OTUs:
    for line in fasta_file:
        if 'OTUnr' in line.split():
            if 'sequence' not in OTU:
                OTU['sequence']=[]
        OTU['sequence'].append(line.split()[0])
print(Vermi_OTUs)

Tags: 数据in文档foriflinefastafile