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

Ejercicio importado del Jupyter Hub

parent 284851ea
Branches master
No related tags found
No related merge requests found
%% 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):
#print con formato
print ("{:<15} {:<20} {:<10} {:<10} {:<15} {:<25} {:<40} {:<10}".format('Name', 'Last Name', 'Age', 'Country', 'City', 'Field', 'Institution', 'Hobby'))
for username in compas:
data_info = compas[username]
if data_info["country"] == country:
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:code id: tags:
``` python
```
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