失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Guardian Part

Guardian Part

时间:2019-07-18 03:32:05

相关推荐

Guardian Part

TIPS

关于Guardian Part

'''用另一种写法来读取mbox-short当中的from'''fhandle = open('mbox-short.txt')for line in fhandle:line = line.rstrip()wds = line.split()if wds[0] != 'From':print('ignore')

这里会报错:

File “E:/TESTS/PYTHON/Guardian_part/guardian_part.py”, line 9, in

if wds[0] != ‘From’:

IndexError: list index out of range

debug:

fhandle = open('mbox-short.txt')for line in fhandle:line = line.rstrip()wds = line.split()print('word: ' , line) #DEBUGif wds[0] != 'From':continueelse:print('From Line')

可以发现源文件当中有空行,这样的话wds[0]本身就是不存在的了

所以解决方法是加保护模块:

'''用另一种写法来读取mbox-short当中的from'''fhandle = open('mbox-short.txt')for line in fhandle:line = line.rstrip()wds = line.split()print('word: ' , line)if len(line) == 0:continue #保护模块if wds[0] != 'From':continueelse:print('From Line')

或者这样写:

fhandle = open('mbox-short.txt')for line in fhandle:line = line.rstrip()wds = line.split()#print('word: ' , line)if len(line) == 0 or wds[0] != 'From':continueelse:print('From Line')

注意:

如果写成

if wds[0] != 'From' or len(line) == 0 :

同样会报错,因为率先执行了wds[0] != ‘From’,所以空行没有被率先排除,所以依然会报错。

如果觉得《Guardian Part》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。