Python:类型' _io.TextIOWrapper'的对象没有len()

时间:2018-02-09 23:45:07

标签: python string function loops

我在运行代码时遇到错误:

  

TypeError:类型' _io.TextIOWrapper'的对象没有len()函数

如何让它打开/读取文件并通过循环运行?

这是我要导入的文件的链接: download link of the DNA sequence

    def mostCommonSubstring():
        dna = open("dna.txt", "r")
        mink = 4
        maxk = 9
        count = 0
        check = 0
        answer = ""
        k = mink
        while k <= maxk:
            for i in range(len(dna)-k+1):
                sub = dna[i:i+k]
                count = 0
                for i in range(len(dna)-k+1):
                    if dna[i:i+k] == sub:
                        count = count + 1
                if count >= check:
                    answer = sub
                    check = count
            k=k+1
        print(answer)
        print(check)

2 个答案:

答案 0 :(得分:0)

由于打开文本文件的方式而出现问题。 您应该在代码中添加dna = dna.read()。 所以你的结束代码应该是这样的:

def mostCommonSubstring():
    dna = open("dna.txt", "r")
    dna = dna.read()
    mink = 4
    maxk = 9
    count = 0
    check = 0
    answer = ""
    k = mink
    while k <= maxk:
        for i in range(len(dna)-k+1):
            sub = dna[i:i+k]
            count = 0
            for i in range(len(dna)-k+1):
                if dna[i:i+k] == sub:
                    count = count + 1
            if count >= check:
                answer = sub
                check = count
        k=k+1
    print(answer)
    print(check)

答案 1 :(得分:0)

@tfabiant:我建议使用此脚本来读取和处理DNA序列。 要在终端中运行此代码: python readfasta.py fastafile.fasta

import string, sys
##########I. To Load Fasta File##############
file = open(sys.argv[1]) 
rfile = file.readline()
seqs = {} 
##########II. To Make fasta dictionary####
tnv = ""#temporal name value
while rfile != "":
    if ">" in rfile:
        tnv = string.strip(rfile)
        seqs[tnv] = ""
    else:
        seqs[tnv] += string.strip(rfile)    
    rfile = file.readline()
##############III. To Make Counts########
count_what = ["A", "T", "C", "G", "ATG"]
for s in seqs:
    name = s
    seq = seqs[s]
    print s # to print seq name if you have a multifasta file
    for cw in count_what:
        print cw, seq.count(cw)# to print counts by seq