简体   繁体   中英

Python for loop storing only the last value

My code are scraping data from public site and storing in a df and saving as csv, but my code are not working well.

    for ano in lista_ano:
     for distribuidora in lista_distribuidores:
      for mes in lista_mes:
        scraping = pd.read_html('https://www2.aneel.gov.br/aplicacoes/indicadores_de_qualidade/decFecSegMensal.cfm?mes={}&ano={}&regiao=SE&distribuidora={}&tipo=d'.format(mes,ano,distribuidora))
        dfs= pd.DataFrame(scraping[0])
        dfs.drop(dfs.tail(3).index,inplace=True)
        dfs.drop(dfs.head(2).index,inplace=True)
        dfs = dfs.assign(MES = '{}'.format(mes))
        dfs = dfs.assign(ANO = '{}'.format(ano))
        dfs = dfs.assign(DISTRIBUIDORA = '{}'.format(distribuidora))
        all_dfs = pd.DataFrame(dfs)
        all_dfs.to_csv('final_data.csv', encoding= 'utf-8')

My problem here is my all_dfs.to_csv are creating a new csv for each looping and not storing data in the same local.

You are overwriting the existing csv on each iteration.

To fix it, simply indicate that you want to append instead of write by

all_dfs.to_csv('final_data.csv', encoding='utf-8', mode='a')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM