IndexError:超出范围的列表索引创建和打开xlsx

时间:2019-04-02 16:46:03

标签: python xlsx

我想用python编写一个程序,该程序创建一个xlsx文件,每个字母都有一张纸。我想读取150个文件,这些文件的数字和名称分别与“:”(例如,“ 03005:stackoverflow”)对应,并在每行中将每个数字及其名称放入以该名称的第一个字母开头的工作表中。

当我执行程序时,它给了我这个错误,我不知道如何解决。并非每次我执行它时,它都会在相同的时间失败。

import openpyxl
import tkinter, re
import xlwt
import os.path
def read_file(name,e,arrayPositions,fileName):
    f = open(name,"r",encoding="utf8")
    print("El fichero que estamos leyendo es "+name)
    for line in f.readlines():
            words = line.split(":")
            if (words[1])[0] is None:
                    if (words[1])[0] == "A":
                            sheet = e.get_sheet("A")
                            sheet.write(arrayPositions[0],0,(words[1])[:-1])
                            sheet.write(arrayPositions[0],1,words[0])
                            arrayPositions[0]=arrayPositions[0]+1
                    if (words[1])[0] == "B":
                            sheet = e.get_sheet("B")
                            sheet.write(arrayPositions[1],0,(words[1])[:-1])
                            sheet.write(arrayPositions[1],1,words[0])
                            arrayPositions[1]=arrayPositions[1]+1
                    if (words[1])[0] == "C":
                            sheet = e.get_sheet("C")
                            sheet.write(arrayPositions[2],0,(words[1])[:-1])
                            sheet.write(arrayPositions[2],1,words[0])
                            arrayPositions[2]=arrayPositions[2]+1
                    if (words[1])[0] == "D":
                            sheet = e.get_sheet("D")
                            sheet.write(arrayPositions[3],0,(words[1])[:-1])
                            sheet.write(arrayPositions[3],1,words[0])
                            arrayPositions[3]=arrayPositions[3]+1
                    if (words[1])[0] == "E":
                            sheet = e.get_sheet("E")
                            sheet.write(arrayPositions[4],0,(words[1])[:-1])
                            sheet.write(arrayPositions[4],1,words[0])
                            arrayPositions[4]=arrayPositions[4]+1
                    if (words[1])[0] == "F":
                            sheet = e.get_sheet("F")
                            sheet.write(arrayPositions[5],0,(words[1])[:-1])
                            sheet.write(arrayPositions[5],1,words[0])
                            arrayPositions[5]=arrayPositions[5]+1
                    if (words[1])[0] == "G":
                            sheet = e.get_sheet("G")
                            sheet.write(arrayPositions[6],0,(words[1])[:-1])
                            sheet.write(arrayPositions[6],1,words[0])
                            arrayPositions[6]=arrayPositions[6]+1
                    if (words[1])[0] == "H":
                            sheet = e.get_sheet("H")
                            sheet.write(arrayPositions[7],0,(words[1])[:-1])
                            sheet.write(arrayPositions[7],1,words[0])
                            arrayPositions[7]=arrayPositions[7]+1
                    if (words[1])[0] == "I":
                            sheet = e.get_sheet("I")
                            sheet.write(arrayPositions[8],0,(words[1])[:-1])
                            sheet.write(arrayPositions[8],1,words[0])
                            arrayPositions[8]=arrayPositions[8]+1
                    if (words[1])[0] == "J":
                            sheet = e.get_sheet("J")
                            sheet.write(arrayPositions[9],0,(words[1])[:-1])
                            sheet.write(arrayPositions[9],1,words[0])
                            arrayPositions[9]=arrayPositions[9]+1
                    if (words[1])[0] == "K":
                            sheet = e.get_sheet("K")
                            sheet.write(arrayPositions[10],0,(words[1])[:-1])
                            sheet.write(arrayPositions[10],1,words[0])
                            arrayPositions[10]=arrayPositions[10]+1
                    if (words[1])[0] == "L":
                            sheet = e.get_sheet("L")
                            sheet.write(arrayPositions[11],0,(words[1])[:-1])
                            sheet.write(arrayPositions[11],1,words[0])
                            arrayPositions[11]=arrayPositions[11]+1
                    if (words[1])[0] == "M":
                            sheet = e.get_sheet("M")
                            sheet.write(arrayPositions[12],0,(words[1])[:-1])
                            sheet.write(arrayPositions[12],1,words[0])
                            arrayPositions[12]=arrayPositions[12]+1
                    if (words[1])[0] == "N":
                            sheet = e.get_sheet("N")
                            sheet.write(arrayPositions[13],0,(words[1])[:-1])
                            sheet.write(arrayPositions[13],1,words[0])
                            arrayPositions[13]=arrayPositions[13]+1
                    if (words[1])[0] == "Ñ":
                            sheet = e.get_sheet("Ñ")
                            sheet.write(arrayPositions[14],0,(words[1])[:-1])
                            sheet.write(arrayPositions[14],1,words[0])
                            arrayPositions[14]=arrayPositions[14]+1
                    if (words[1])[0] == "O":
                            sheet = e.get_sheet("O")
                            sheet.write(arrayPositions[15],0,(words[1])[:-1])
                            sheet.write(arrayPositions[15],1,words[0])
                            arrayPositions[15]=arrayPositions[15]+1
                    if (words[1])[0] == "P":
                            sheet = e.get_sheet("P")
                            sheet.write(arrayPositions[16],0,(words[1])[:-1])
                            sheet.write(arrayPositions[16],1,words[0])
                            arrayPositions[16]=arrayPositions[16]+1
                    if (words[1])[0] == "Q":
                            sheet = e.get_sheet("Q")
                            sheet.write(arrayPositions[17],0,(words[1])[:-1])
                            sheet.write(arrayPositions[17],1,words[0])
                            arrayPositions[17]=arrayPositions[17]+1
                    if (words[1])[0] == "R":
                            sheet = e.get_sheet("R")
                            sheet.write(arrayPositions[18],0,(words[1])[:-1])
                            sheet.write(arrayPositions[18],1,words[0])
                            arrayPositions[18]=arrayPositions[18]+1
                    if (words[1])[0] == "S":
                            sheet = e.get_sheet("S")
                            sheet.write(arrayPositions[19],0,(words[1])[:-1])
                            sheet.write(arrayPositions[19],1,words[0])
                            arrayPositions[19]=arrayPositions[19]+1
                    if (words[1])[0] == "T":
                            sheet = e.get_sheet("T")
                            sheet.write(arrayPositions[20],0,(words[1])[:-1])
                            sheet.write(arrayPositions[20],1,words[0])
                            arrayPositions[20]=arrayPositions[20]+1
                    if (words[1])[0] == "U":
                            sheet = e.get_sheet("U")
                            sheet.write(arrayPositions[21],0,(words[1])[:-1])
                            sheet.write(arrayPositions[21],1,words[0])
                            arrayPositions[21]=arrayPositions[21]+1
                    if (words[1])[0] == "V":
                            sheet = e.get_sheet("V")
                            sheet.write(arrayPositions[22],0,(words[1])[:-1])
                            sheet.write(arrayPositions[22],1,words[0])
                            arrayPositions[22]=arrayPositions[22]+1
                    if (words[1])[0] == "W":
                            sheet = e.get_sheet("W")
                            sheet.write(arrayPositions[23],0,(words[1])[:-1])
                            sheet.write(arrayPositions[23],1,words[0])
                            arrayPositions[23]=arrayPositions[23]+1
                    if (words[1])[0] == "Y":
                            sheet = e.get_sheet("Y")
                            sheet.write(arrayPositions[24],0,(words[1])[:-1])
                            sheet.write(arrayPositions[24],1,words[0])
                            arrayPositions[24]=arrayPositions[24]+1
                    if (words[1])[0] == "X":
                            sheet = e.get_sheet("X")
                            sheet.write(arrayPositions[25],0,(words[1])[:-1])
                            sheet.write(arrayPositions[25],1,words[0])
                            arrayPositions[25]=arrayPositions[25]+1
                    if (words[1])[0] == "Z":
                            sheet = e.get_sheet("Z")
                            sheet.write(arrayPositions[26],0,(words[1])[:-1])
                            sheet.write(arrayPositions[26],1,words[0])
                            arrayPositions[26]=arrayPositions[26]+1
   e.save(fileName)
   f.close()
   return arrayPositions


fileName = "aCode.xlsx"
e = xlwt.Workbook(fileName)
e.add_sheet("A")
e.add_sheet("B")
e.add_sheet("C")
e.add_sheet("D")
e.add_sheet("E")
e.add_sheet("F")
e.add_sheet("G")
e.add_sheet("H")
e.add_sheet("I")
e.add_sheet("J")
e.add_sheet("K")
e.add_sheet("L")
e.add_sheet("M")
e.add_sheet("N")
e.add_sheet("Ñ")
e.add_sheet("O")
e.add_sheet("P")
e.add_sheet("Q")
e.add_sheet("R")
e.add_sheet("S")
e.add_sheet("T")
e.add_sheet("U")
e.add_sheet("V")
e.add_sheet("W")
e.add_sheet("X")
e.add_sheet("Y")
e.add_sheet("Z")
arrayPositions = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

for x in range(1,150):
        arrayPositions = read_file(str(x)+".txt",e,arrayPositions, fileName)

1 个答案:

答案 0 :(得分:2)

您有

if (words[1])[0] is None:

,然后将words[1][0]的值与某些字符进行比较。

您是要写

if (words[1])[0] is not None:

因此,如果这些值不是NoneType,它将进入inner if并将该值与字符进行比较

更新

当然不能解决您的问题,就像列表单词没有元素[1]或单词[1] [0]没有元素会给您索引错误。有很多方法可以查看具有该索引的列表是否存在。但是对您来说,最简单的方法是将if (words[1])[0] is None:更改为try:,并在if语句后写入except:。例如:

if (words[1])[0] is None:
                if (words[1])[0] == "A":
                        sheet = e.get_sheet("A")
                        sheet.write(arrayPositions[0],0,(words[1])[:-1])
                        sheet.write(arrayPositions[0],1,words[0])
                        arrayPositions[0]=arrayPositions[0]+1
                **all other ifs

try:
    if (words[1])[0] == "A":
        sheet = e.get_sheet("A")
        sheet.write(arrayPositions[0],0,(words[1])[:-1])
        sheet.write(arrayPositions[0],1,words[0])
        arrayPositions[0]=arrayPositions[0]+1
    ** All other if statements
except:
    pass