我收到索引错误作为列表超出范围。我必须扫描很多行 [英] I am getting an Index error as list out of range. I have to scan through many lines

查看:147
本文介绍了我收到索引错误作为列表超出范围。我必须扫描很多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  import nltk 
从nltk.tokenize导入随机
导入sent_tokenize,word_tokenize


file = open(sms .txt,r)
for line in file:
#print line
a = word_tokenize(line)
if a [5] =='SBI'和a [ 6] =='借记':
打印一份[13]

任何人都可以请帮我纠正错误。程序运行几行然后停止并给出索引超出范围错误。我理解错误,但我不知道如何解决它。我想基本上删除不可读的行。

解决方案

只需添加列表长度检查即可解决问题。<如果len(a)> = 14且a [5] =='SBI'和a [6] =='借方':
打印[13]


import nltk
import random
from nltk.tokenize import sent_tokenize, word_tokenize


file = open("sms.txt", "r")
for line in file:
    #print line
    a=word_tokenize(line)
    if a[5] == 'SBI' and a[6]== 'Debit':
        print a[13]

Could any one please help me in rectifying the error. The programme runs for a few lines then stops and gives an index out of range error. I understand the error but I dont know how to fix it. I want to basically remove the lines which have are not readable.

解决方案

Just add a list length check would solve the problem.

if len(a) >= 14 and a[5] == 'SBI' and a[6]== 'Debit':
    print a[13]

这篇关于我收到索引错误作为列表超出范围。我必须扫描很多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆