""" Purpose: introductory take on examining a table of values """ # get access to web-based data support import url # we cannot access web data without the url module # set repository folder DATASET_FOLDER = "http://www.cs.virginia.edu/~cs1112/datasets/csv/" # you could go to this folder and download the datasets that we use in class here # get name of the dataset reply = input( "Enter name of dataset: " ) file_name = reply.strip() # get rid of any accidental unwanted whitespace # specify link link = DATASET_FOLDER + file_name # when we're getting info from the class webpage, we will have folder+file to make our link # get dataset table = url.get_dataset( link ) # get_dataset() vs. get_contents() - if we want a big string, use get_contents() # if you want a dataset/csv file, use get_datasets() # typically it will be clear which function you should use from problem to problem # "read a dataset" -> get_dataset() # "read a page" -> get_contents() # url.py has a lot of functions, and it will parse a csv into a dataset # since csv stands for comma-separated file, it will use split( "," ) to split on the commas # these functions are very handy and you don't really need to know how they work print( "table:", table ) # this will print as a big ugly list of lists, because that's all a dataset is to python print() # determine number of rows in table nrows = len( table ) # since a dataset is a list of lists, the length is the number of row lists in the table # this returns our number of lists in the megalist # if all rows have the same number of columns, ncols = len( table[0] ) print( "the table has", nrows, "rows" ) print() # print each row of the table along with its number of columns for row in table : # row is our loop variable that takes on a different row in table each run # you should always be able to identify what type of data is stored in your loop variable # in this case, row will be a list every time, so we can use list methods ncols = len( row ) # length of a row is the length of the inner list we're looking at # this hands back the number of elements in the row print( "row", row, "has", ncols, "columns" ) print() # print each row of the table using row indices for r in range( 0, nrows ) : # r is now going to be an integer every run of the for loop row = table[ r ] # we use that integer to access a specific row # this allows us to keep track of the row number, which can be very useful # *row is going to be the same in both of these loops* print( "row", r, ":", row ) # if you're working on a problem and you're not able to do it by looping through the # list, try transitioning to this type of list and see if you can solve the problem # python probably turns our looping through lists, strings, etc. into looping through # ranges and working with indices