Python - "Error: IndexError: list index out of range"

1

I can not find the cause of the error. The program runs fine, it displays all the rows of the table, however, this error message appears.

import re
import xlrd

wb = xlrd.open_workbook('exemplo.xls', encoding_override="cp1252", ragged_rows=True) # enconding_override remove o erro de ausência de condificação em XLS antigos.
worksheet = wb.sheet_by_index(0)
n = 1
while worksheet.cell(n,0).value != xlrd.empty_cell.value: # You can detect an empty cell by using empty_cell in xlrd.empty_cell.value
    # Captura o nome completo da paciente na planilha.
    nomecompleto = worksheet.cell(n, 0).value
    # Expressão Regular para isolar o primeiro nome. Utilizar nome.group(0) para eliminar <_sre.SRE_Match object at
    nome = re.search(r'([A-Z]*)\s', nomecompleto)
    # ZENVIA Layout Tipo B: to;message;from
    print(worksheet.cell(n,2).value + ';' + 'Senhora ' + nome.group(0) + 'a CLI confirma sua consulta com o(a) médico(a) ' +
      worksheet.cell(n,3).value + ' no dia ' + worksheet.cell(n,4).value +
      '. Responda gratis S para confirmar ou N para cancelar.' + ';' + 'CLI')
    n = n + 1

Bug Integra:

Traceback (most recent call last):   File "C:/Users/Thiago/Dropbox/Python/PySmartDoctor/readexcel/readexcel.py", line 12, in <module>
    while worksheet.cell(n,0).value != xlrd.empty_cell.value: # You can detect an empty cell by using empty_cell in xlrd.empty_cell.value  File "C:\Users\Thiago\AppData\Local\Programs\Python\Python35-32\lib\site-packages\xlrd\sheet.py", line 401, in cell
    self._cell_types[rowx][colx], IndexError: list index out of range
    
asked by anonymous 17.07.2016 / 18:27

1 answer

1

Solution, I put the following condition inside the loop:

 if n >= worksheet.nrows:
        break
    
17.07.2016 / 19:48