【发布时间】:2021-04-27 15:00:28
【问题描述】:
我正在尝试扫描多个文件并在同一行中搜索两个关键字。我正在尝试在同一行中查找关键字“SEQADV”和“MUTATION”。问题是我不断收到错误“NameError: name 'wt_residue' is not defined”。当我搜索一个关键字“SEQADV”时,程序运行流畅。
if 'SEQADV' and 'MUTATION' in line:
try:
mutation = line.split()
sequence_number = mutation[4]
chain = mutation[3]
mutant_residue = mutation[2]
wt_residue = mutation[7]
except IndexError:
pass
#Prints all data to the .csv file above and closes the file
print(",".join([pdb_name, mutant_residue, chain, sequence_number, wt_residue]), file=datafile)
datafile.close()
【问题讨论】:
-
这一行是不正确的
if 'SEQADV' and 'MUTATION' in line:如果代码碰到了except 块,wt_residue也是未声明的 -
except IndexError: pass是一个非常糟糕的主意。在分配之前,您可能会遇到错误。去掉try,看看有没有报错。 -
好的。我删除了除 IndexError: pass 之外的内容,但随后我收到另一个错误消息“列表索引超出范围”。请问我该如何解决 if 语句?
-
if 'SEQADV' and 'MUTATION' in line:等价于if 'MUTATION' in line:。'SEQADV'被评估为布尔表达式,并且在该上下文中始终被评估为True。
标签: python python-3.x csv