简体   繁体   English

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

[英]python; how to write output to text file

With my code, I loop over files and count patterns in files. 通过我的代码,我遍历文件并计算文件中的模式。 My code is as follows 我的代码如下

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)

And my output looks like this 我的输出看起来像这样

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

The code works fine, but I cannot write my output to a file. 该代码工作正常,但我无法将输出写入文件。 I get the following error: 我收到以下错误:

    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

What am I doing wrong and how can I solve this problem, to make sure that my code write the output to a txt file? 我在做什么错,如何解决此问题,以确保我的代码将输出写入txt文件?

The original verions of count_kmers() did not contain a return statement, which means it has an implicit return None . count_kmers()的原始版本不包含return语句,这意味着它具有隐式return None

As you assign this to counting all of your errors became self explanatory. 当您将其分配给counting所有错误时,它们就变得很容易理解。

After your edit, the end of the function looked like this: 编辑之后,函数的结尾如下所示:

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

which returns a tuple with four values. 返回具有四个值的元组。 It also exits the function on the first pass through the loop. 它还在第一次通过循环时退出该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM