浅笑心心 2017-03-07 09:30 采纳率: 25%
浏览 1428

IndexError: list index out of range

写这个程序原本想提取下文本text中http的个数,但是python中运行程序总出现这个问题,并且时好时坏,有时能运行,有时报错,谁能帮忙解决下,谢谢

from __future__ import division
import json
import codecs
import sys
import string
import os,random
import re
import shutil
import jieba
import jieba.analyse
reload(sys)
sys.setdefaultencoding("utf-8")

class Comment:
    def __init__(self,id,mid,text):
        self.id = id
        self.mid = mid
        self.text = text
    def getid(self):
        return self.id
    def getmid(self):
        return self.mid
    def gettext(self):
        return self.text

def readallcomments(inputfile):
    AllComments = []
    commentFile = open(inputfile)
    for line in commentFile.readlines():
        sep = line.split('\t')
        comment = Comment(sep[0],sep[1],sep[2])
        AllComments.append(comment)
    commentFile.close()
    return AllComments

if __name__ == '__main__':
    file=open(r'E:\\project\\myOutput.txt')
    rumorcommentpath = 'E:\\project\\gerenjieshao\\'
    rumorhttppath = 'E:\\project\\result\djym\\'
    for line in file:
        line=line.strip('\n')
        outfile = open(os.path.join(rumorhttppath,line+'.txt'),'w')
        inputfile = rumorcommentpath + line + '.txt'
        allcomments = []
        allcomments = readallcomments(inputfile)
        for i in allcomments: 
            http = re.findall("(?isu)(http\://[a-zA-Z0-9\.\?/&\=\:]+)",i.gettext())
            outfile.writelines(str(i.getid())+'\t'+str(i.getmid())+'\t'+str(len(http))+'\r\n')
            print 'processing...'+line
        outfile.close()

  • 写回答

2条回答 默认 最新

  • oyljerry 2017-03-07 11:08
    关注

    数组越界了,你检查一下出错的时候对应的代码行数,是不是你给数组的下标超过数组长度了

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?