Subido por Ulises Cura Jauregui

Ejemplo Python+PySimpleGUI con lectura de archivos csv

Anuncio
Python+PySimpleGUI con lectura de archivo csv
#!/usr/bin/env python
# -*- coding: utf-8 -*import csv
import PySimpleGUI as sg
import random
def leo_arch(nombre):
dictionary = dict()
with open(nombre, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
next(reader)
for row in reader:
university = row[2]
valor = (int(row[10]) if row[10] is not '' else 0)
if university not in dictionary:
dictionary[university] = valor
else:
dictionary[university]+= valor
return dictionary
# for university,cant in dictionary.items():
#
print("{university} tiene {count} mujeres estudiando.".format(university=university,
count=cant))
#
def sort_by_score(valores):
return(sorted(valores.items(), key=lambda y: y[1], reverse=True))
main_layout = [
[sg.Text('Archivo')], [sg.Input(key="file"), sg.FileBrowse(), sg.OK(key="OK"),
sg.Button('Cancelar')],
[sg.Listbox(values=[], key='universidades', size=(60, 10))],
[sg.Graph(canvas_size=(400, 400), graph_bottom_left=(0, 0), graph_top_right=(400, 400),
background_color='red', key='graph')],
[sg.ReadButton('Ordenar', key='_Listo_', disabled=True)]
]
def dibujo(valores):
cant_max = (valores[0][1])
tam_max = 75
propor = tam_max / int(cant_max)
print(propor)
graph = window.FindElement('graph')
for cada in valores:
tam = propor * cada[1]
pos = (random.randrange(400), random.randrange(400))
circle = graph.DrawCircle(pos, tam, fill_color='black', line_color='white')
window = sg.Window('Window Title').Layout(main_layout)
datos = []
lei_arch = False
while True:
event, values = window.Read()
if event is None or event == 'Cancelar':
break
if event is event == 'OK':
window.FindElement('_Listo_').Update(disabled=False)
datos.append(leo_arch(values["file"]))
data=list(datos[0].items())#elementos del diccionario
window.FindElement('universidades').Update(data)
lei_arch = True
window.FindElement('OK').Update(disabled=True)
if event == '_Listo_' and lei_arch:
datos_ordenados = sort_by_score(datos[0])
dibujo(datos_ordenados)
window.FindElement('universidades').Update(datos_ordenados)
Descargar