Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
423 views
in Technique[技术] by (71.8m points)

Python elif: syntax error

Hi guys I have a syntax error that I really don't understand... Anyone can help? I get this message on the console:

File "./data_4.0.1.py", line 170
elif:    
   ^
SyntaxError: invalid syntax

The error is raised on this elif:

#controllo ultimo timestamp    
    elif:    
        with open(nomen, 'rb') as f:
            last_timestamp = f.readlines()[-1].split(",")[0]

Here is my code:

def funzione_aggiornamento_prezzi(titolo,timeframe,lookback):


#parametri per scaricare lo storico dei prezzi

    if timeframe =='TBT':
            lookback = 0

    elif timeframe =='1M':
            lookback = 7

    elif timeframe =='5M':
            lookback = 60

    elif timeframe =='60M':
            lookback = 180

    elif timeframe =='1D':
            lookback = 1800 


    params = {'item': titolo, 
              'frequency': timeframe,
              'dataDa':x_giorni_fa(lookback)} 

    try:
         r = requests.get(myurl, params=params)
    except:
         pprint("Si e' verificato un errore")
    else:
        pprint(r.status_code)
        pprint(r.url)        
        new_list = crea_lista(r)           

#codice per scrivere su di un csv da una lista
    nomen = "%s.%s.csv" % (titolo,timeframe)
    csvfile = open(nomen, 'a')    
    reportwriter = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
#codice per scrivere su di un csv

#controllo del numero di rows nel file
    with open(nomen,"r") as f:
        reader = csv.reader(f,delimiter = ",")
        data = list(reader)
        row_count = len(data)

    if row_count == 0:
        for i in new_list:
            da_appendere = i
            reportwriter.writerow(da_appendere)
    csvfile.close() 

#controllo ultimo timestamp    
    elif:    
        with open(nomen, 'rb') as f:
            last_timestamp = f.readlines()[-1].split(",")[0]

#codice per appendere solo i nuovi dati
        found_it = 0

        for i in range((len(new_list))-1):
                if new_list[i] == last_timestamp:   
                    found_it = 1 
                if found_it == 1:
                    this_elem = new_list[i]
                    next_elem = new_list[(i+1)]
                    #print(this_elem)
                    #print(next_elem)
                    da_appendere1 = next_elem
                    reportwriter.writerow(da_appendere1)

        csvfile.close()

for i in lista_indici:
            for j in lista_timeframe:

                funzione_aggiornamento_prezzi(i,j,lookback)  
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you end the if block in the previous line when put a instruction at the same level indentation that the if statement

if condition:
    stuff
something # doing this close the if block

and a elif can only happen in a if block

and you do that in

    if row_count == 0:
        for i in new_list:
            da_appendere = i
            reportwriter.writerow(da_appendere)
    csvfile.close() #<-- here you close the if block

#controllo ultimo timestamp    
    elif:    #<-- you forgot the condition, and is outside of a 'if' block 
        with open(nomen, 'rb') as f:
            last_timestamp = f.readlines()[-1].split(",")[0]

futhermore you forget to put a condition in the elif, if you don't need one use else instead


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...