Hello
about 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, does anyone have an idea of the problem?
The original csv file I searched for is the following: link
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})