Skip to content

Instantly share code, notes, and snippets.

@christophergregory
christophergregory / solution_1.py
Created August 27, 2014 14:24
Python 101 Assignment 3 Solution 1
people = {} # let's start with an empty dictionary to hold our data
with open('numbers.txt', 'r') as numbers:
for index, line in enumerate(numbers):
if index > 0: # skip the first line (don't need the headers)
name, birthday = line.rstrip().split(',')
# study this line carefully, it's intentionally terse
people.update({ name : { 'number' : birthday } })
@christophergregory
christophergregory / solution_2.py
Last active August 29, 2015 14:05
Python 101 Assignment 3 Solution 2
import csv
# Open all required files with at once
with open('numbers.txt', 'r') as numbersfile, \
open('birthdays.txt', 'r') as birthdayfile, \
open('output.txt', 'w') as output:
# Read in each line as a dictionary
# https://docs.python.org/2/library/csv.html#csv.DictReader
numReader = csv.DictReader(numbersfile, fieldnames=['Name','Number'])