gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:45:51 26 4
gpt4 key购买 nike

运行我的代码时,我不断收到错误消息:

TypeError: object of type '_io.TextIOWrapper' has no len() function



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

这是我尝试导入的文件的链接:
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)

最佳答案

出现问题的原因是您打开文本文件的方式。
您应该添加 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)

关于Python: '_io.TextIOWrapper' 类型的对象没有 len(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48715691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com