【发布时间】:2017-05-21 09:17:20
【问题描述】:
我有一个如下的文本文件。
l[0]l[1]l[2]l[3]l[4]l[5]l[6]
-----------------------------------
1| abc is a book and cba too
2| xyz is a pencil and zyx too
3| def is a pen and fed too
4| aaa is
实际文件是:
abc is a book and cba too
xyz is a pencil and zyx too
def is a pen and fed too
aaa is
我正在使用下面的代码对该文本文件执行操作:
import sys
fr = open("example.txt",'r')
for l in fr:
if(l[3] is "book" or l[3] is "pencil")
Then do something
if(l([3] is "pen")
Then do something
fr.close()
当我尝试执行这个程序时,我遇到了类似的错误
Traceback(most recent call last):
File "abc.py" line 4 in <module>
if(l[3] is "book" or l[3] is "pencil"):
IndexError: list index error out of range
因为根据上述文本文件的最后一行(即第 4 行),l[3] 处没有任何内容
l[0] l[1] l[2] l[3] l[4] l[5] l[6]
aaa是
这里的第 4 行 l[3] 是空的。 所以我的问题是当 l[3] 为空时如何跳过这一行? 我们可以像下面这样露营吗
if(l[3] ==""):
continue
请有人在这里帮助我。
【问题讨论】:
-
请格式化您的问题,以便我们尝试了解您的问题。
标签: python python-2.7 python-3.x