read table data
Marathon Magnets Solution
Here’s some code that reads the raw data from from the CSV data file. The column headings from the first line are loaded into a list called column_headings. The rest of the data (all the rows of times) are loaded into a dictionary called row_data, with each row of data keyed with the row label string from the start of each line. Of course, as luck would have it, someone was cleaning the fridge door, and they’ve left a bunch of magnets on the floor. You were to see if you could arrange the magnets into their correct order.
You need to be sure to create an dictionary for the row times. empty
row_data = {}
Create the column headings from the first line of data.
with open('PaceData.csv') as paces:
column_headings =
paces.readline().strip().split(',')
ip it Read a line from the file, strthe n of unwanted whitespace, and Delete the first split the line on comma. heading, the Process the rest of the file. for each_line in paces: “V02”string—you don’t need it. row = each_line.strip().split(',') It’s the same deal here: take the line, strip it, and l = row.pop(0) row_labe then split on comma. Extract the row label. row_data[row_label] = row Use the row label num_cols = len(column_headings) together with the rest print(num_cols, end=' -> ') of the line’s data to upd ate the dictionary. print(column_headings) column_headings.pop(0)
num_2mi = len(row_data['2mi']) print(num_2mi, end=' -> ') print(row_data['2mi']) num_Marathon = len(row_data['Marathon']) print(num_Marathon, end=' -> ') print(row_data['Marathon'])
404 Chapter 11