小编Hal*_*Hal的帖子

其他选项而不是使用try-except

当文本文件中的第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)

python file filter try-except

8
推荐指数
1
解决办法
1134
查看次数

标签 统计

file ×1

filter ×1

python ×1

try-except ×1