search

Home  >  Q&A  >  body text

python这段编程提示 list index out of range求解

这段编程总提示list index out of range,看半天想不通~求解代码如下


def saveData(datalist,savepath):

book=xlwt.Workbook(encoding='utf-8',style_compression=0)
sheet=book.add_sheet('豆瓣最受欢迎影评',cell_overwrite_ok=True)
col=('标题','作者','影片名','影片详情链接','推荐级','回应数','影评链接','影评','有用数')
for i in range(0,9):
    sheet.write(0,i,col[i])#列名
for i in range(0,50):#总共50条影评
    data=datalist[i]
    for j in range(0,9):
        sheet.write(i+1,j,data[j])#数据
book.save(savepath)#保存

def main():

baseurl='http://movie.douban.com/review/best/?start='
datalist=getData(baseurl)
savapath=u'豆瓣最受欢迎影评.xlsx'
saveData(datalist,savapath)

main()

伊谢尔伦伊谢尔伦2866 days ago1202

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:13:10

    The list is out of bounds. Are you sure the length of each datalist is 9? Is it possible that when crawling a web page, some data is missing and not assigned a value? As a result, each datalist may have different lengths. Cause for j in range(0,9) When data[j] goes out of bounds?

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:13:10

    I agree with the above. The information given by the poster is incomplete and there is no contextual information about the error. You can only print it out and compare it yourself.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:13:10

    The list is out of bounds, why didn’t I post the error message

    reply
    0
  • Cancelreply