Python elif:语法错误 [英] Python elif: syntax error

查看:77
本文介绍了Python elif:语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个语法错误,我真的不明白……有人可以帮忙吗?我在控制台上收到此消息:

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

此 elif 出现错误:

The error is raised on this 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)  

推荐答案

当把指令放在与 if 语句相同级别的缩进时,你结束上一行的 if 块

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

elif 只能发生在 if 块中

and a elif can only happen in a if block

你在

    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]

此外,您忘记在 elif 中添加条件,如果您不需要,请使用 else 代替

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

这篇关于Python elif:语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆