Skip to content
Snippets Groups Projects
Commit 7b7c28ff authored by Carla Elena Gomez Alvarado's avatar Carla Elena Gomez Alvarado
Browse files

Este archivo es la version final del ejercicio 3.

parent 0dc9c6b5
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
Nombre: Carla Gomez
Estudiante Lic. Física
Universidad Simón Bolívar
Caracas, Venezuela
**Ejercicio 3: Amigos Congeros**
La solución se plantea en una serie de funciones, y para efectos de facilidad en el manejo de la data se unió toda en un .csv, y se asignaron índices para las columnas.
Las funcines para el Ejercicio planteado fueron:
*read_file : para leer el archivo .csv con la información.
*main: es la función que llama a todas las demás.
*get_info_by_country: De acuerdo a un pais ingresado, imprime todos los datos de compas en ese pais, organizados con formato.
*get_age_avg: saca el promedio de las edades por pais
*get_intitutions: imprime todas las instituciones por pais.
%% Cell type:code id: tags:
``` python
#importar librerias a usar
import csv, json
#En esta variable se guardan todas las filas del CSV
compas = {}
#previamente he cargado el archivo cega_tarea_2.csv en el directorio del pwd
FILE_NAME = 'cega_tarea_2.csv'
#Indices para cada columna
USERNAME = 0
NAME = 1
LASTNAME = 2
AGE = 3
COUNTRY = 4
CITY = 5
FIELD = 6
INSTITUTION = 7
HOBBY = 8
```
%% Cell type:code id: tags:
``` python
#Funcion para leer un archivo
def read_file():
#Abre el archivo .csv
with open(FILE_NAME) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
#Saltarse los encabezados de las columnas
header = True
#Para cada fila del archivo .csv
for row in csv_reader:
if not header:
compas[row[USERNAME]] = {
"name" : row[NAME],
"lastname" : row[LASTNAME],
"age" : row[AGE],
"country" : row[COUNTRY],
"city" : row[CITY],
"field" : row[FIELD],
"institution" : row[INSTITUTION],
"hobby" : row[HOBBY],
}
else:
header = False
```
%% Cell type:code id: tags:
``` python
# Funcion principal
def main():
read_file()
print('--------------------------')
get_info_by_country('Colombia')
print('--------------------------')
print(f'Age avg: {get_age_avg()}')
print('--------------------------')
print(f'Institutions: {get_institutions()}')
```
%% Cell type:code id: tags:
``` python
def get_info_by_country(country):
#Encabezados
print ("{:<15} {:<20} {:<10} {:<10} {:<15} {:<25} {:<40} {:<10}".format('Nombre', 'Apellido', 'Edad', 'Pais', 'Ciudad', 'Rama', 'Institucion', 'Hobby'))
for username in compas:
data_info = compas[username]
if data_info["country"] == country:
#Imprime la informacion por cada fila encontrada
print ("{:<15} {:<20} {:<10} {:<10} {:<15} {:<25} {:<40} {:<10}".format(data_info["name"], data_info["lastname"], data_info["age"], data_info["country"], data_info["city"], data_info["field"], data_info["institution"], data_info["hobby"]))
```
%% Cell type:code id: tags:
``` python
def get_age_avg():
if len(compas) == 0:
return 0
ages_sum = 0
for username in compas:
ages_sum += int(compas[username]["age"])
return ages_sum / len(compas)
```
%% Cell type:code id: tags:
``` python
def get_institutions():
institutions = set()
for username in compas:
institutions.add(compas[username]["institution"])
return institutions
```
%% Cell type:markdown id: tags:
Estas lineas son para ejecución en terminal. Aca en el Jupyter hub dan error por la forma del archivo .csv
%% Cell type:code id: tags:
``` python
#if __name__ == "__main__":
#main()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment