当文本文件中的第2行具有"nope"时,它将忽略该行并继续下一行.是否有另一种方法来写这个没有使用尝试,除了?我可以使用if else语句来执行此操作吗?
文本文件示例:
0 1
0 2 nope
1 3
2 5 nope
Run Code Online (Sandbox Code Playgroud)
码:
e = open('e.txt')
alist = []
for line in e:
start = int(line.split()[0])
target = int(line.split()[1])
try:
if line.split()[2] == 'nope':
continue
except IndexError:
alist.append([start, target])
Run Code Online (Sandbox Code Playgroud)