2016-04-03 13 views
-3

こんにちは、私は本当に理解できない構文エラーがあります...誰でも手伝っていただけますか? 私はコンソールにこのメッセージが表示されます。Python elif:構文エラー

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

エラーがこのelifのに発生します。ここ

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

は私のコードです:

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) 
+0

'elif'を条件とせずに単純に' else'にする必要があります。 –

答えて

1

if声明

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

elif場合にのみブロック

で発生する可能性がありますし、あなたが行うのと同じレベルのインデントの命令を置くときあなたは、前の行であればブロックを終了あなたが1を必要としない場合は、elifに条件を置くことを忘れ

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でいます代わりにelseを使用してください。

+0

今すぐ使える!どうもありがとう !!! –

+0

私はcsvfile.closeをforループのレベルで動かし、elifステートメントをelseで変更しました –

+0

@DiegoDiTommaso:これがあなたの質問に答えたなら、それを受け入れることを検討するべきです。 [ヘルプ]の[誰かが私の質問に答えたらどうなるでしょうか?](http://stackoverflow.com/help/someone-answers)を参照してください。 – usr2564301

0

あなたは別のものを持っていない場合if文の場合、elifの代わりにelseを使用してください。
制御フローに関してはdocumentationを参照してください。