【发布时间】:2020-06-25 00:44:20
【问题描述】:
我正在处理 pset6,DNA 问题。此代码适用于 small.cvs,但当我尝试使用大代码时,它高估了 STR 计数。我想问题出在它尝试比较字符串时。但仍然不知道如何修复它。我检查了“TTTTTTCT”序列的计数是否正确,但对于剩余的 STR,计数在所有情况下都大于应有的值。
import sys
import csv
def main():
while (len(sys.argv) != 3):
print ("ERROR. Usage: python dna.py data.csv sequence.txt")
break
list_str = {}
#load the STRs to analyse
with open(sys.argv[1]) as csvfile:
readcsv = csv.reader (csvfile)
ncol = len(next(readcsv))
csvfile.seek(0)
header = list()
for line in readcsv:
a = sum(1 for line in readcsv)
for i in range(ncol):
list_str[line[i]] = 0
header.insert (i, line [i])
print (f"{header[i]}")
#open an work with the sequence file
sequence = open(sys.argv[2], 'r')
seq_r = sequence.read()
for k in list_str.keys():
#print (f"keu {k}")
p = 0
seq = len(seq_r)
while p < seq:
if seq_r[p:(p + len(k))] == k:
list_str[k] += 1
p += len(k)
else: p += 1
#print (f" sequenci encontrada{list_str[k]} y {k}")
print (f"nro de {k} {list_str[k]}")
with open(sys.argv[1]) as csvfile:
readcsv = csv.reader (csvfile)
next(csvfile)
find = False
for row in readcsv:
for j in range(1,ncol):
#print(f"header :{header[j]}")
if int(row [j]) == int(list_str[header[j]]):
print (f"row {row[j]} list {list_str[header[j]]}")
find = True
else:
find = False
break
if find == True: print (f"{row [0]}")
main()
【问题讨论】:
-
在第一个 csv 读取的第二个 for 循环中没有缩进问题吗?照原样,第二个循环只在最后一行工作
-
嗨哈维尔,在你写下一个问题之前,请先熟悉asking questions!享受您在 SO 的住宿:)