Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
On error > Try again
#1
Lightbulb 
Hello guys,

I've the following code with a Try and Except statement, as you can see below:
import csv
import time
import sched, time
from datetime import datetime
from FlightRadar24.api import FlightRadar24API

fr_api = FlightRadar24API()
empresas = ['ACL', 'AJB', 'AMP', 'ANS', 'ARE', 'ARG', 'AUZ', 'AVA', 'AZN', 'AZP', 'AZU', 'BOL', 'BOV', 'DAP', 'ECO', 'EFY', 'ETR', 'FBZ', 'GAL', 'GCA', 'GLG', 'GLO', 'JAT', 'JES', 'KRE', 'LAE', 'LAN', 'LAU', 'LAV', 'LCO', 'LER', 'LNE', 'LPE', 'LTG', 'MWM', 'NAA', 'NSE', 'PAM', 'PTB', 'RER', 'ROI', 'RPB', 'RUC', 'SID', 'SKU', 'SRU', 'TAM', 'TGY', 'TIW', 'TPA', 'TTL', 'TUY', 'TVU', 'VCV', 'VEC', 'VNE', 'VPE', 'VVC', 'WAY']

while True:
    try:
        now = datetime.now()
        day = datetime.today()
        current_time = now.strftime("%H:%M")
        current_day = day.strftime("%d/%m/%y")
        day_saving = day.strftime("%d%m%y")
       
        for i in range(0,len(empresas)):
            flights = fr_api.get_flights(airline = empresas[i])
            #print(type(flights[0]))
            #print(flights[0].__dict__)

            print("Dados da empresa: ", empresas[i], " atualizado as: ", current_time)

            with open(r'C:\Users\Anelise\Desktop\Voos\DB\Voos' + str(day_saving) + '.txt', 'a', newline='') as f:
                fieldnames = ['id', 'dia_analisado',
                              'hora_analisada',
                              'data',
                              'hora',
                              'airline_icao',
                              'callsign',
                              'aircraft_code',
                              'registration',
                              'origin_aiport_iata',
                              'destination_airport_iata',
                              'latitude',
                              'longitude',
                              'heading',
                              'altitude',
                              'ground_speed',
                              'on_ground',
                              'vertical_speed',
                              'squawk'                              
                              ] # you can include whatever attributes you want
                wrtr = csv.DictWriter(f, fieldnames=fieldnames, extrasaction='ignore', delimiter=';')
                #wrtr.writeheader()
                for flight in flights:
                    flight.data = datetime.fromtimestamp(flight.time).strftime("%d/%m/%y") # convert the timestamp to datetime object
                    flight.hora = datetime.fromtimestamp(flight.time).strftime("%H:%M:%S")
                    flight.hora_analisada = current_time
                    flight.dia_analisado = current_day
                    wrtr.writerow(flight.__dict__) # here I pass all attributes as dict, but you can always access individual attributes, like I did with time above
        time.sleep(60)  
    except:
        print('Error, try again!')
        time.sleep(10)  
So, every time we get an error, the system starts from the first item on empresas list. How can I change it to restart from the stop point? I mean, if there was an error with the company called GLO, I would like to try again starting from GLO, and not from the first one ACL.

How can I do that?
Reply
#2
Move your try: to line 19, and then fix the indentation so except matches.
Reply
#3
Just an FYI.

A lot of the support data for your application can be found here: https://ourairports.com/data

and data-dictionary for same here: https://ourairports.com/help/data-dictionary.html

homepage:

This data is updated every night.

I also know that there is a major contributor from basil, and if that's you, thank you.
Reply


Forum Jump:

User Panel Messages

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