''' Purpose: practice getting data from a useful dataset ''' # define the base folder for course csv datasets # THIS BASE LINK!! CSV_WEB_FOLDER = "http://www.cs.virginia.edu/~cs1112/datasets/csv/" ############### BOILER PLATE TO GET CONTENTS OF WEB FILE ############### # need help to get web data - so import the capability from url import get_contents # 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 text = get_contents( link ) print( text ) ############### TURN CONTENTS INTO A LIST OF LINES ############### # turn the text into data; i.e., a list of lines lines = text.split( "\n" ) print( "Lines=", lines ) ############### TURN LINES INTO A DATASET -- LIST OF LISTS ############### # get csv rows into dataset from dataset = [] # OUTER LIST FOR OUR LIST OF LISTS - We will put more lists # into this larger list! for line in lines : # clean up the line line = line.strip() # split the line to cells cells = line.split(",") # Why do we split on comma? We get # each thing separated in a line and put it into a list # so that we can use everything in that list for our rows that # we're building up. print( "Cells=", cells) row = [] for cell in cells : # clean up the cell ... # add the cell to the row ... # add the row of data to our dataset ... # print the dataset for row in dataset : print( row )