【发布时间】:2018-07-20 19:20:32
【问题描述】:
我在运行我的代码时不断收到错误:
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)
【问题讨论】:
-
你不能打电话给
len(dna) -
文件对象没有
len。它们也不能被切片/索引,即dna[i:i+k]也会失败
标签: python string function loops