In [1]:
import random #package for generating pseudo-random numbers:

In [3]:
import csv

In [4]:
person = raw_input('Enter your name: ')

Enter your name: Richard

In [5]:
place = raw_input('Enter your destination: ')

Enter your destination: Chelsea

In [6]:
r = random.randrange(0,11) # generate random number between 0 and 10

In [8]:
excuse_list = [] #create an empty list to hold the excuses
inputReader = csv.DictReader(open('excuse.csv','rU'))
for line in inputReader:
    excuse_list.append(line) # append the excuses (as dictionary) to the list

In [7]:
print "Sorry, " + person + " I was late to " + place + ", " + excuse_list[r]['excuse']
print 'From the story "' + excuse_list[r]['headline'] + '"'
print excuse_list[r]['hyperlink']

Sorry, Richard I was late to Chelsea, breakfast began a little later than expected
From the story "De Blasio 15 Minutes Late to St. Patrick's Day Mass, Blames Breakfast"

In [15]:
# alternate way of generating the list of excuses using the context manager
excuse_list2 = []
with open('excuse.csv','rU') as inputFile:
    inputReader = csv.DictReader(inputFile)
    for line in inputReader:
        excuse_list2.append(line) # append the excuses (as dictionary) to the list
    #file connection is close at end of the indented code

In [ ]:
# This is the least elegant and least pythonic way of doing this. 
# Putting this code up at a Python conference could get you booed or otherwise shamed and driven from the hall
# but it gets the job done
inputFile = open('excuse.csv','rU') #create the file object
header = next(inputFile) # return the first line of the file (header) and assign to a variable
excuse_list = []
for line in inputFile:
    line = line.split(',') # split the line on the comma
    excuse_list.append(line[0]) # append the first element to the list
inputFile.close() # close connection to the file

In [ ]: