{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## CSV Input/Output in Python" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Creating a list of lists from scratch\n", "# Each 'row' contains a number, its square, and its cube.\n", "\n", "exponent_table = []\n", "\n", "for i in range(10):\n", " row = [i, i**2, i**3]\n", " exponent_table.append(row)\n", "\n", "exponent_table" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Writing the list of lists to a CSV file\n", "\n", "import csv\n", "\n", "out_path = \"Exponent_table.csv\"\n", "\n", "header = ['Number', 'Squared', 'Cubed']\n", "\n", "with open(out_path, 'w') as fo:\n", " csv_writer = csv.writer(fo)\n", " csv_writer.writerow(header)\n", " csv_writer.writerows(exponent_table)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Viewing the contents of the CSV file we just created\n", "\n", "!cat Exponent_table.csv" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Downloading a CSV containing The Guardian's top 10 'Greatest Arthouse and Drama' films\n", "## https://www.theguardian.com/news/datablog/2010/oct/16/greatest-films-of-all-time\n", "\n", "!wget https://raw.githubusercontent.com/pcda17/pcda17.github.io/master/week/7/Greatest_Arthouse_Drama_Films.csv" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Loading the CSV as a list of lists\n", "\n", "import csv\n", "\n", "csv_path = \"Greatest_Arthouse_Drama_Films.csv\"\n", "list_of_lists = []\n", "\n", "with open(csv_path) as fi:\n", " csv_input = csv.reader(fi)\n", " for row in csv_input:\n", " list_of_lists.append(row)\n", "\n", "list_of_lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(list_of_lists)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Viewing the header row\n", "\n", "list_of_lists[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Viewing a single record's row\n", "\n", "list_of_lists[8]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Viewing the 'director' field in the row we viewed above\n", "\n", "row = list_of_lists[8]\n", "\n", "row[2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Viewing the header row alongside each field's index\n", "\n", "list(enumerate(list_of_lists[0]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Creating a reduced version of a single row, containing 4 selected fields\n", "\n", "row = list_of_lists[9]\n", "\n", "film = row[1]\n", "director = row[2]\n", "year = row[4]\n", "country = row[8]\n", "\n", "reduced_row = [film, director, year, country]\n", "\n", "reduced_row" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Creating a new list of lists containing reduced versions of each row\n", "\n", "reduced_list_of_lists = []\n", "\n", "for row in list_of_lists:\n", " film = row[1]\n", " director = row[2]\n", " year = row[4]\n", " country = row[8]\n", " reduced_row = [film, director, year, country]\n", " reduced_list_of_lists.append(reduced_row)\n", "\n", "reduced_list_of_lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Writing the reduced list of lists to a CSV\n", "\n", "out_path = \"Greatest_Arthouse_Drama_Films_reduced.csv\"\n", "\n", "with open(out_path, 'w') as fo:\n", " csv_writer = csv.writer(fo)\n", " csv_writer.writerows(reduced_list_of_lists)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Viewing the contents of the CSV file we just created\n", "\n", "!cat Greatest_Arthouse_Drama_Films_reduced.csv" ] }, { "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.5.2" } }, "nbformat": 4, "nbformat_minor": 2 }