Skip to content

Instantly share code, notes, and snippets.

@jazzido
Created August 9, 2011 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jazzido/1133176 to your computer and use it in GitHub Desktop.
Save jazzido/1133176 to your computer and use it in GitHub Desktop.
Convertir escrutinios electorales elección Gobernador pcia Córdoba 2011
# convertir escrutinios de eleccion gobernador 2011 pcia de Cordoba a un CSV
# Script horrible.
# requiere la presencia de algunos archivos en el directorio en donde se ejecuta
# para obtenerlos:
#
# $ wget http://www.eleccordoba2011.com.ar/iniele/listas.xml
# $ wget http://www.eleccordoba2011.com.ar/iniele/secciones.xml
# $ for i in `seq 1 26`; do wget http://www.eleccordoba2011.com.ar/resultados/Seccion_`printf %02d $i`_Municipio_000_Circuito_000_Cargo_01.xml; done
#
# Copyright (c) <2011> <Manuel Aristaran - http://jazzido.com>
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the
# Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions of
# the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import sys
from xml.etree import ElementTree
import csv
listas_file = ElementTree.parse('listas.xml')
listas = dict([(cnode.attrib['idtabla'], cnode.attrib['des'],)
for cnode in listas_file.getiterator('cnode')])
secciones_file = ElementTree.parse('secciones.xml')
secciones = [snode.attrib for snode in secciones_file.getiterator('snode')]
rv = []
for s in secciones:
idtabla = int(s['idtabla'])
if idtabla == 0: continue
seccion_nombre = filter(lambda x: x['idtabla'] == s['idtabla'], secciones)[0]['des']
row = { 'departamento': seccion_nombre }
res_file = ElementTree.parse("Seccion_%02d_Municipio_000_Circuito_000_Cargo_01.xml" % idtabla)
for rnode in [rnode.attrib for rnode in res_file.getiterator('rnode')]:
row[listas[rnode['idLista']]] = int(rnode['Votos'])
rv.append(row)
def flatten(l):
return [item for sublist in l for item in sublist]
import unicodedata
def strip_accents(s):
return ''.join((c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn'))
writer = csv.writer(open('resultados-gobernador-cordoba.csv', 'wb'))
headers = [unicode(x[0]).encode('utf-8') for x in sorted(row.items(), key=lambda x: x[0])]
tmp = zip(headers[:-1], ['PCT ' + x for x in headers[:-1]])
tmp = flatten(tmp)
tmp.extend(['TOTAL VOTOS', headers[-1]])
writer.writerow(tmp)
for row in rv:
tmp = [unicode(x[1]).encode('utf-8') for x in sorted(row.items(), key=lambda x: x[0])]
total_votos = sum([int(x) for x in tmp[:-1]])
total_votos_positivos = sum([int(x) for x in tmp[:-5]])
tmp_percent = ["%.2f" % (float(x) * 100 / float(total_votos_positivos)) for x in tmp[:-1]]
row_tmp = zip(tmp[:-1], tmp_percent)
row_tmp = flatten(row_tmp)
row_tmp.append(total_votos)
row_tmp.append(strip_accents(tmp[-1].decode('utf-8')))
writer.writerow(row_tmp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment