【发布时间】:2020-03-07 02:52:02
【问题描述】:
我需要将 dict 的 key(DNA_Base) 和 value(number) 与 csv 文件的一些数据进行比较并打印匹配的数据。
问题是 csv 文件有 3 个东西一个人名,一个字符串(DNA_Base)和一个数字它应该打印具有此特定DNA_Base 的特定编号的人的姓名。我想比较的字典是STR_max 应该是这样的
STR_max = {'AATG': 8 , 'TATC': 10 , 'AGATC': 9 , 'AGAG': 13}
所以它应该为这个 csv 文件打印 Alice,如果没有匹配将打印一些文本
名称,AGATC,AATG,TATC 爱丽丝,2,8,3 鲍勃,4,1,5 查理,3,2,5这是我的代码
import sys
from sys import argv
import csv
#check correct command line argument
if len(sys.argv) != 3:
print("Usage: python dna.py data.csv sequence.txt")
exit(1)
#get the file path from the command line argument
csv_path = argv[1]
seq_path = argv[2]
# Opens csv file
with open(csv_path, newline='') as csvfile:
readcsv = csv.reader(csvfile)
# Gets accsess to STR names
csv_rows = list(readcsv)
str_names = csv_rows[0]
# Opens the DNA sequence
seqtxt = open(seq_path, "r")
str_seq = seqtxt.read()
#Dict so store the counting of str
STR_max = {}
#iterate over the STR of the database
for str_name in str_names[1:]:
maxCount = 0
actualCount = 0
str_name_len = len(str_name)
str_seq_len = len(str_seq)
i = 0
found = False
#iterate over the DNA Seq and count the str_name
while i < str_seq_len:
#find the STR in range of str_name[i : i+str_name_len]
find = str_seq.count(str_names, i, i + str_name_len)
#if the 1st STR found then start counting from it
if find > 0 and found == False:
actualCount = 1
i = i + str_name_len
found = True
#if another STR is found again next to the previous one
elif find > 0 and found == True:
actualCount += 1
i = i + str_name_len
else:
i += 1
found = False
if actualCount > maxCount:
maxCount = actualCount
#adding the STR and its maxCount to a buffer dict
STR_max[str_name] = maxCount
【问题讨论】:
-
你的问题是什么?
-
如何将字典的这些项目与csv文件的数据进行比较,并提取匹配的名称
-
您具体在哪个部分苦苦挣扎?
-
该部分未包含在代码中,因为我不知道该怎么做,所以我的问题是如何将字典中的项目与 csv 文件的数据进行比较跨度>
标签: python csv dictionary