1
hello
on the python code below, I want to create a csv with the sum of inhabitants in each state
but it creates an empty csv file
please, someone has idea of the problem?
the original csv file in which I searched the data is this: https://drive.google.com/open?id=0B72DpG1W01nneHU5UXh0TzdSSVU
code:
import csv
brasil = csv.DictReader(open('municipios-brasil.csv', encoding='utf-8'))
total = {}
for municipio in brasil:
estado = municipio['estado']
habitantes = int(municipio['habitantes'])
if estado not in total:
total[estado] = 0
total[estado] = total[estado] + habitantes
arquivo = open('habitantes.csv', mode = 'w', encoding = 'utf-8')
resultado = csv.DictWriter(arquivo, fieldnames = ['estado', 'habitantes'])
resultado.writeheader()
for estado, habitantes in total.items():
resultado.writerow({'estado': estado, 'habitantes':habitantes})
Thank you very much!
– Reinaldo Chaves
@Reinaldochaves, consider marking my answer as accepted, so the staff can find it and review
– Guilherme Marthe
Sure, it worked. But I don’t know where to take the answers
– Reinaldo Chaves
It’s a tick under the voting box ^_^
– Guilherme Marthe