a9q2 - def csv_list(filename): try: f = file(filename, 'r')...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
## CS 116 Ru Wang r46wang 20319247 ## Assignment 9 Question 2 ##csv_list: string -> (union (listof (listof string)) none) ## purpose: the function consumes a string representing the name of the file in csv format and produces a list of list of string with each list within the list representing a line in the file. ## Effects: the function will display "Could not open the file" if an invalid file is entered ## examples: ## csv_list("") will display "Could not open the file" ## csv_list("a9q2input.csv") should return ## [[ÓJackÓ, ÓSmithÓ, ÓCSÓ, ÓONÓ], [ÓAliceÓ, ÓAdamÓ, ÓMathÓ, ÓSKÓ], [ÓJeanÓ, ÓGuyÓ, ÓPHYÓ, ÓQCÓ], [ÓKimÓ, ÓKingÓ, ÓENGLÓ, ÓNSÓ]]
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: def csv_list(filename): try: f = file(filename, 'r') except: print "Could not open the file" return mylines = lines = f.readlines() for l in lines: newline = l.strip() los = newline.split(",") nlos = for s in los: nlos.append(s.strip()) mylines.append(nlos) f.close() return mylines ## Testing: ## empty test: print "Test 1" print " 'Could not open the file' should be displayed" print "The output follows: " csv_list("") ## csv_list("a9q2input.csv") print "Test 2" expect = [["Jack", "Smith", "CS", "ON"], ["Alice", "Adam", "Math", "SK"], ["Jean", "Guy", "PHY", "QC"], ["Kim", "King", "ENGL", "NS"]] ans = csv_list("a9q2input.csv") print ans == expect...
View Full Document

This note was uploaded on 10/21/2010 for the course CS 116 taught by Professor T.vasiga during the Fall '09 term at Waterloo.

Ask a homework question - tutors are online