{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "! head -5 ice-cream.csv" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "\"\"\"The simplest data reader.\"\"\"\n", "\n", "for line in open('ice-cream.csv'):\n", " row = line.split(',')\n", " print(row)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def read_data(path):\n", " \"\"\"Reads comma separated data from path.\"\"\"\n", " infile = open(path)\n", " infile.readline() # discard headers\n", " for line in infile:\n", " line = line.strip()\n", " row = line.split(',')\n", " print(row)\n", "\n", "read_data('ice-cream.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "'1011' / 10" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "row = ['1011', 'f', 't']\n", "assert row[1] in 'mf' and row[2] in 'ps'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "line = '\"damon, matt\", m, p'\n", "line.split(',')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import csv\n", "\n", "def read_data(path):\n", " \"\"\"Reads comma separated data from path.\"\"\"\n", " with open(path) as csvfile:\n", " reader = csv.DictReader(csvfile)\n", " for row in reader:\n", " assert row['gender'] in 'mf'\n", " assert row['preference'] in 'ps'\n", " print(row['gender'], row['preference'])\n", "\n", "read_data('ice-cream.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "counts = {\n", " 'm': \n", " {'s': 8,\n", " 'p': 12},\n", " 'f':\n", " {'s': 16,\n", " 'p': 4}\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "counts['f']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "counts['f']['p']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import csv\n", "from collections import defaultdict\n", "\n", "def ice_cream_counts(path):\n", " \"\"\"Returns gender/preference counts from csv in path.\"\"\"\n", " counts = {'m': defaultdict(int), 'f': defaultdict(int)}\n", "\n", " with open(path) as csvfile:\n", " reader = csv.DictReader(csvfile)\n", " for row in reader:\n", " counts[row['gender']][row['preference']] += 1\n", "\n", " return counts\n", "\n", "counts = ice_cream_counts('ice-cream.csv')\n", "print(counts)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def write_html(counts):\n", " flavors = {'p': 'pistachio', 's': 'strawberry'}\n", "\n", " heading = \"\"\"\n", "
| preference | female | male |
|---|---|---|
| {} | {} | {} |