蟒蛇;如何将输出写入文本文件

时间:2017-09-25 19:33:45

标签: python

使用我的代码,我循环遍历文件并计算文件中的模式。我的代码如下

from collections import defaultdict
import csv, os, re
from itertools import groupby
import glob


   def count_kmers(read, k):
        counts = defaultdict(list)
        num_kmers = len(read) - k + 1
        for i in range(num_kmers):
            kmer = read[i:i+k]
            if kmer not in counts:
                counts[kmer] = 0
            counts[kmer] += 1
        for item in counts:
            return(basename, sequence, item, counts[item])

    for fasta_file in glob.glob('*.fasta'):
        basename = os.path.splitext(os.path.basename(fasta_file))[0]
        with open(fasta_file) as f_fasta:
            for k, g in groupby(f_fasta, lambda x: x.startswith('>')):
                if k:
                    sequence = next(g).strip('>\n')
                else:
                    d1 = list(''.join(line.strip() for line in g))
                    d2 = ''.join(d1) 
                    complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
                    reverse_complement = "".join(complement.get(base, base) for base in reversed(d1))
                    d3 = list(''.join(line.strip() for line in reverse_complement))
                    d4 = ''.join(d3)
                    d5 = (d2+d4)
                    counting = count_kmers(d5, 5)
                    with open('kmer.out', 'a') as text_file:
                        text_file.write(counting)

我的输出看起来像这样

1035 1 GAGGA 2
1035 1 CGCAT 1
1035 1 TCCCG 1
1035 1 CTCAT 2
1035 1 CCTGG 2
1035 1 GTCCA 1
1035 1 CATGG 1
1035 1 TAGCC 2
1035 1 GCTGC 7
1035 1 TGCAT 1

代码工作正常,但我无法将输出写入文件。我收到以下错误:

    TypeError                                 Traceback (most recent call last)
<ipython-input-190-89e3487da562> in <module>()
     37                 counting = count_kmers(d5, 5)
     38                 with open('kmer.out', 'w') as text_file:
---> 39                     text_file.write(counting)

TypeError: write() argument must be str, not tuple

我做错了什么,如何解决这个问题,以确保我的代码将输出写入txt文件?

1 个答案:

答案 0 :(得分:5)

count_kmers()的原始版本不包含return语句,这意味着它具有隐式return None

当您将此内容分配给counting时,所有错误都会自我解释。

编辑完成后,函数的结尾如下所示:

for item in counts:
    return(basename, sequence, item, counts[item])

返回一个包含四个值的元组。它也会在第一次通过循环时退出函数。