''' Purpose: practice getting data from a useful dataset ''' # define the base folder for course csv datasets CSV_WEB_FOLDER = "http://www.cs.virginia.edu/~cs1112/datasets/csv/" import url # get name of the dataset reply = input( "Enter name of dataset: " ) print() # clean up the reply to get file name file_name = reply.strip() # get url link for dataset link = CSV_WEB_FOLDER + file_name # get contents of web resource text = url.get_contents( link ) # clean-up the text text = text.strip() #this is the dataset, but we need to split it into lines #print( 'text =', text ) ############### TURN CONTENTS INTO A LIST OF LINES ############### # turn the text into data; i.e., a list of lines lines = text.split( "\n" ) #we can tell .split() to split on anything we want, in this case we telling it to split on new lines #print('lines =', lines) #we're almost ready to work with it ############### TURN LINES INTO A DATASET -- LIST OF LISTS ############### # get csv rows into dataset from dataset = [] #we will fill it below for line in lines : # clean up the line line = line.strip() # split the line to cells cells = line.split(',') #print( 'cells =' , cells ) #cell is individual number in each line #they're separated by commas so that is what we will split on row = [] #we will use this to hold our cleaned up cells for cell in cells : # clean up the cell cell = cell.strip() # add the cell to the row row.append( cell ) #now we will add it to row # add the row of data to our dataset dataset.append( row ) #print('row =', row) # decompose dataset into header and data header = dataset[ 0 ] data = dataset[ 1 : ] # print the header print( "header:" ) print( header ) print() # print the dataset data print( "data:" ) for row in dataset : print( row )