如何将python中for循环的输出写入csv格式的文件?

2024-06-16 08:25:13 发布

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

下面是python脚本,它标识在不同文件的列表中是否找到某些单词。在

experiment=open('potentiation.txt')
lines=experiment.read().splitlines()
receptors=['crystal_1.txt', 'modeller_1.txt', 'moe_1.txt',
           'nci5_modeller0000_1.txt', 'nci5_modeller0001_1.txt',
           'nci5_modeller0002_1.txt', 'nci5_modeller0003_1.txt',
           'nci5_modeller0004_1.txt', 'nci5_modeller0005_1.txt',
           'nci5_modeller0006_1.txt', 'nci5_modeller0007_1.txt',
           'nci5_modeller0008_1.txt', 'nci5_modeller0009_1.txt',
           'nci5_modeller0010_1.txt', 'nci5_modeller0011_1.txt',
           'nci5_moe0000_1.txt', 'nci5_moe0001_1.txt', 'nci5_moe0002_1.txt',
           'nci5_moe0003_1.txt', 'nci5_moe0004_1.txt', 'nci5_moe0005_1.txt',
           'nci5_moe0006_1.txt', 'nci5_moe0007_1.txt', 'nci5_moe0008_1.txt',
           'nci5_moe0009_1.txt', 'nci5_moe0010_1.txt', 'nci5_moe0011_1.txt',
           'nci5_moe0012_1.txt', 'nci5_moe0013_1.txt', 'nci5_moe0014_1.txt']

for ligand in lines:
    for protein in receptors:
        file1=open(protein,"r")
        read1=file1.read()
        find_hit=read1.find(ligand)
        if find_hit == -1:
            print ligand,protein,"Not Found"
        else:
            print ligand,protein, "Found"

此代码的输出示例如下:

^{pr2}$

我的问题是如何将输出格式化为一个类似下面示例的csv文件?在

Ligand  nci5_moe0012_1. nci5_moe_0013_1   nci5_moe_0014
345647  Not Found        Not Found        Found

Tags: 文件txtreadnotopenfindligandexperiment
2条回答

我认为这样的方法可以做到(假设输出文件是制表符分隔的):

import csv
import os

receptors = ['crystal_1', 'modeller_1', 'moe_1',
             'nci5_modeller0000_1', 'nci5_modeller0001_1',
             'nci5_modeller0002_1', 'nci5_modeller0003_1',
             'nci5_modeller0004_1', 'nci5_modeller0005_1',
             'nci5_modeller0006_1', 'nci5_modeller0007_1',
             'nci5_modeller0008_1', 'nci5_modeller0009_1',
             'nci5_modeller0010_1', 'nci5_modeller0011_1',
             'nci5_moe0000_1', 'nci5_moe0001_1', 'nci5_moe0002_1',
             'nci5_moe0003_1', 'nci5_moe0004_1', 'nci5_moe0005_1',
             'nci5_moe0006_1', 'nci5_moe0007_1', 'nci5_moe0008_1',
             'nci5_moe0009_1', 'nci5_moe0010_1', 'nci5_moe0011_1',
             'nci5_moe0012_1', 'nci5_moe0013_1', 'nci5_moe0014_1']

with open('potentiation.txt', 'rt') as experiment, \
     open('output.csv', 'wb') as outfile:
    csv_writer = csv.writer(outfile, delimiter='\t')
    csv_writer.writerow(['Ligand'] + receptors)  # header row
    for ligand in (line.rstrip() for line in experiment):
        row = [ligand]
        for protein in receptors:
            with open(protein+'.txt', "rt") as file1:
                found = ['Found', 'Not Found'][file1.read().find(ligand) == -1]
                row.append(found)
        csv_writer.writerow(row)

print('output.csv file written')

更新

正如我在一篇评论中所说,只需读一次蛋白质文件,就可以快得多。为了能够做到这一点并以您想要的方式格式化输出,每个文件中每个配体的检查结果都需要存储在一个数据结构中,这个数据结构是在每个文件被读取和检查多次时逐步建立起来的,结果在所有操作完成之后,一次都被写出来。一份简单的清单清单就足以满足这一目的,并已在下文的实施中使用。在

取而代之的是使用更多的内存,而不是一遍又一遍地阅读蛋白质文件。由于磁盘IO通常是计算机上速度最慢的东西之一,所以只需稍微增加一点代码复杂度,就可以获得巨大的性能提升。在

下面是显示此替代版本的代码:

^{pr2}$

将“蛋白质”和“配体”的值添加到适当的列表(在0索引中)后,可以将结果保存在列表中(一个列表用于配体,一个用于蛋白质)。在轻松保存文本文件之后。
要保存,请打开一个文件以写入并转换字符串列表:

my_string = " ".join(map(str, lst))

然后保存我的字符串 (并对每个列表执行此操作)

相关问题 更多 >