Search code examples
pythonxlsx

IndexError: list index out of range creating and opening a xlsx


I want to make a program in python that creates an xlsx file that has a sheet for each letter. I want to read 150 files that have numbers and name separate for ":", for example "03005:stackoverflow" on each line, and put each number with its name in the sheet that starts with the first letter of the name.

When I execute my program it gives me this error and I don't know how to fix it. Not every time I execute it, it fails at the same interation.

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)

Solution

  • you have

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

    in your code and then you compare values of words[1][0] to some characters.

    Did you mean to write

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

    So, that if these values are not NoneType it will go to inner if and compare the value with characters

    UPDATE

    Of couse that would not solve your problem, as if list word does not have element [1] or words[1][0] does not have element it will give you index error. There are many ways to see if list with that index exists. But for you the easiest one would be to change if (words[1])[0] is None: with try: and write except: after the if statements. For example:

    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
    

    to

    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