0
I am new in Python and am learning to analyze and write CSV files. I’m stuck in a problem I haven’t been able to solve for a few days: I’m trying to read a government database, also in CSV, take some information, calculate it, and write a new CSV with the ordered information. (To be exact: I’m counting the number of cases of feminicide and attempted feminicide per year and the ratio between them. The idea is to create a four-column CSV with this data) The code is as follows::
import csv
import collections #biblioteca collections
#abre o arquivo
arq = open('fem.csv')
# cria o contador vazio
num = collections.Counter()
num2 = collections.Counter()
#laço for > itera no dicionário criado com o método DictReader do arquivo
for x in csv.DictReader(arq, delimiter=';') :
num[x['ano']] += int(x['feminicidio_tentativa'])
num2[x['ano']] += int(x['feminicidio'])
#print(num.most_common()) #most_common() recebe um parametro que limita o ranking
#criando um arquivo CSV com esses dados
arquivo_s = open('arquivo_saida.csv', mode='w', encoding = 'utf8')
esc = csv.writer(arquivo_s, delimiter=';', lineterminator='\n')
esc.writerow(['ano', 'feminicidio_tentativa', 'feminicidio', 'razão fem/tent'])
#itera em num e escreve ano, feminicidio nas primeiras duas colunas do csv
for ano,feminicidio_tentativa in num.items() :
esc.writerow([ano, feminicidio_tentativa])
# escreve o dado ONDE ELE QUISER ESSE ANARQUISTA DO *******
for ano,feminicidio in num2.items() :
esc.writerow(['','', feminicidio, feminicidio/feminicidio_tentativa])
arquivo_s.close()
print('ok')
The exit, however, stays like this:
Someone knows what I’m doing wrong?
Hello... What is the structure of the 'fem.csv' file'?
– Marcos Alexandre
The file is a spreadsheet also in csv
– Raposa
Fox, good night! Place a test dataset with your fem.csv. Hug!
– lmonferrari