diff --git a/Ejercicio3.ipynb b/Ejercicio3.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..73b67917a780b18ed8cb89922ee0017c32be1e29 --- /dev/null +++ b/Ejercicio3.ipynb @@ -0,0 +1,153 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "#importar librerias a usar \n", + "\n", + "import csv, json\n", + "\n", + "#En esta variable se guardan todas las filas del CSV\n", + "compas = {}\n", + "\n", + "#previamente he cargado el archivo cega_tarea_2.csv en el directorio del pwd \n", + "\n", + "FILE_NAME = 'cega_tarea_2.csv'\n", + "\n", + "#Indices para cada columna\n", + "USERNAME = 0\n", + "NAME = 1\n", + "LASTNAME = 2\n", + "AGE = 3\n", + "COUNTRY = 4\n", + "CITY = 5\n", + "FIELD = 6\n", + "INSTITUTION = 7\n", + "HOBBY = 8\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "#Funcion para leer un archivo\n", + "def read_file():\n", + " #Abre el archivo .csv\n", + " with open(FILE_NAME) as csv_file:\n", + " csv_reader = csv.reader(csv_file, delimiter=',')\n", + " #Saltarse los encabezados de las columnas\n", + " header = True\n", + " #Para cada fila del archivo .csv\n", + " for row in csv_reader:\n", + " if not header:\n", + " compas[row[USERNAME]] = {\n", + " \"name\" : row[NAME],\n", + " \"lastname\" : row[LASTNAME],\n", + " \"age\" : row[AGE],\n", + " \"country\" : row[COUNTRY],\n", + " \"city\" : row[CITY],\n", + " \"field\" : row[FIELD],\n", + " \"institution\" : row[INSTITUTION],\n", + " \"hobby\" : row[HOBBY],\n", + " }\n", + " else:\n", + " header = False" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Funcion principal\n", + "def main():\n", + " read_file()\n", + " print('--------------------------')\n", + " get_info_by_country('Colombia')\n", + " print('--------------------------')\n", + " print(f'Age avg: {get_age_avg()}')\n", + " print('--------------------------')\n", + " print(f'Institutions: {get_institutions()}')" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def get_info_by_country(country):\n", + " #print con formato\n", + " print (\"{:<15} {:<20} {:<10} {:<10} {:<15} {:<25} {:<40} {:<10}\".format('Name', 'Last Name', 'Age', 'Country', 'City', 'Field', 'Institution', 'Hobby')) \n", + " for username in compas:\n", + " data_info = compas[username]\n", + " if data_info[\"country\"] == country:\n", + " 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", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def get_age_avg():\n", + " if len(compas) == 0:\n", + " return 0\n", + " ages_sum = 0\n", + " for username in compas:\n", + " ages_sum += int(compas[username][\"age\"])\n", + "\n", + " return ages_sum / len(compas)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def get_institutions():\n", + " institutions = set()\n", + " for username in compas:\n", + " institutions.add(compas[username][\"institution\"])\n", + " \n", + " return institutions" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}