Python Forum
IndexError: list index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: list index out of range
#1
Hello guys...

What's going on with my code?
from tempfile import NamedTemporaryFile
from FlightRadar24.api import FlightRadar24API
from time import time, sleep
from datetime import datetime
import shutil
import csv
from pandas import DataFrame
import re
import threading
import schedule 
import time 
import sched, time

nav = []
n_acft_voando = []
while True:    

    fr_api = FlightRadar24API()
    empresas = ['AZU', 'GLO', 'LAN']
    #, 'JBU', 'AAL', 'DAL', 'ACA', 'UAL'
    pattern = "<(.*?)>"

    agora = datetime.now()
    dia = agora.strftime("%d/%m/%Y")
    hora = agora.strftime("%H:%M:%S")

    for i in range(0,len(empresas)):
        nav.append(len(fr_api.get_flights(airline = empresas[i])))
        dados_voo = fr_api.get_flights(airline = empresas[i])

        for k in range (0,len(dados_voo)):
            substring = re.search(pattern, str(dados_voo[k])).group(1)
            substring = substring.split(' - ')
            modelo_tail = [x.replace(" ", ";") for x in substring]
            n_acft_voando = [dia, hora, nav[0], nav[1], nav[2]]
            acft_voando=[dia, hora, modelo_tail[0]]
            print(acft_voando)

            with open(r'C:\Users\bruno\Desktop\Voos\DB\AeronavesAr.txt', 'a', newline='') as f:
                writer = csv.writer(f, delimiter=';')
                writer.writerow(n_acft_voando)

            with open(r'C:\Users\bruno\Desktop\Voos\DB\DetalhesAeronaves.txt', 'a', newline='') as f:
                writer = csv.writer(f, delimiter=';')
                writer.writerow(acft_voando)

    sleep(60)
I'm getting the following error:
Error:
IndexError Traceback (most recent call last) <ipython-input-2-aac74b35f2b2> in <module> 33 substring = substring.split(' - ') 34 modelo_tail = [x.replace(" ", ";") for x in substring] ---> 35 n_acft_voando = [dia, hora, nav[0], nav[1], nav[2]] 36 acft_voando=[dia, hora, modelo_tail[0]] 37 print(acft_voando) IndexError: list index out of range
How can I fix it?
It's interesting because, smetimes it works, and sometimes it doesn't.
Reply


Messages In This Thread
IndexError: list index out of range - by brunolelli - Mar-25-2021, 04:43 AM
RE: IndexError: list index out of range - by buran - Mar-25-2021, 02:13 PM
RE: IndexError: list index out of range - by buran - Mar-25-2021, 02:26 PM
RE: IndexError: list index out of range - by buran - Mar-25-2021, 04:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  index of range, but data prints out mrc06405j 1 321 Mar-25-2021, 07:20 PM
Last Post: buran
  Changing Index of 2 List in python giddyhead 0 298 Mar-05-2021, 05:45 PM
Last Post: giddyhead
  IndexError: list index out of range ramu4651 2 519 Jan-24-2021, 01:45 PM
Last Post: buran
  string index out of range jade_kim 4 451 Jan-13-2021, 05:41 AM
Last Post: jade_kim
  IndexError: index 10 is out of bounds for axis 0 with size 1 vahid89 1 1,475 Jan-07-2021, 06:19 PM
Last Post: deanhystad
Question Matching variable to a list index Gilush 17 997 Nov-30-2020, 01:06 AM
Last Post: Larz60+
  How to get index of minimum element between 3 & 8 in list Mekala 2 710 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 627 Nov-04-2020, 11:26 AM
Last Post: Aggam
  List index out of range error while accessing 2 lists in python K11 2 568 Sep-29-2020, 05:24 AM
Last Post: K11
  list vs [] with range quazirfan 5 672 Sep-19-2020, 12:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020