I have a csv file similar to this one, with all the information of all municipalities in Brazil (I shortened the csv to not be too long):
ESTADO,MUNICIPIO,HABITANTES,AREA
AC,ACRELÂNDIA,12538,1807.92
AC,ASSIS BRASIL,6072,4974.18
AC,BRASILÉIA,21398,3916.5
AC,BUJARI,8471,3034.87
AL,BATALHA,17076,320.92
AL,BELÉM,4551,48.63
AM,BARCELOS,25718,122476.12
AM,BARREIRINHA,27355,5750.57
AM,BENJAMIN CONSTANT,33411,8793.42
I'm trying to add just the number of inhabitants of the northern region, in this case, AC and AM. For this I used the code below (in Python 3.6.5):
import csv
populacao = 0
arquivo = open('brasil.csv', encoding='utf8')
for registro in csv.reader(arquivo):
habitantes = registro[2]
estado = registro[0]
if habitantes != 'habitantes':
if estado != 'estado':
regiao_norte = ['AC', 'AM']
for estado in regiao_norte:
populacao += int(habitantes)
print(populacao)
I get as a sum: 381511598. But the sum is clearly incorrect. I thought using the list would act as a picker of the states I wanted to add. I can not understand what I'm missing. How can I make this sum correctly?