''' Purpose: get the contents of a CSV file ''' # get access to python web resources import urllib.request # name the location of the data repository BASE_URL = 'http://www.cs.virginia.edu/~cs1112/datasets/csv/' # get the name of the dataset reply = input( 'Enter the name of a dataset: ' ) name = reply.strip() # specify where in the repository we can find the dataset web_file = BASE_URL + name # connect the program to the web resource stream = urllib.request.urlopen( web_file ) # read the web resource contents = stream.read() # decode text to put in standard form text = contents.decode( 'UTF-8') # clean up text text = text.strip() # split text into a list of lines lines = text.split( '\n' ) # convert lines to rows pf data dataset = [] # need to process each line for line in lines: # clean up line line = line.strip() # split the line's comma-separated values into a list of data elements csv_line = line.split( ',' ) # add another row of data to the dataset dataset.append( csv_line ) # show the dataset for row in dataset : print( row )