Load Scores (Solution)
I have this source data with player scores already open on the left and saved to a local file named
scores.csv. Because this file is located in my current working directory, I won’t have to specify the full path in Python in order to open it, just the filename. By the way, if you haven’t downloaded this file yet, then you can do so now or copy and paste the corresponding values from the slides, as there are only a dozen or so rows in that file.
Over on the right, I have an empty Python module named
challenge.py, where I’ll be writing my solution. The first step is to load the data on the left into Python, so I’ll need the usual set of imports—that is, the
csv module that will allow me to read and write CSV files, as well as
Path object for manipulating file paths.
This is the input file. Because later I’ll need to define yet another path for the output file, I’ll assign this one to a variable named
scores_path, which is slightly more descriptive than just
path that I’ve been using in the review exercises before.
Let me assign those values to local variables in the loop’s body. First,
name is equal to
row_dict["name"]. Then the corresponding score is equal to
row_dict["score"], and I’ll need to pass that to the built-in
02:53 Okay, now I have the name of a player and the numeric score of that player at a given time. The next step is to find the highest score of all players and keep them in a leaderboard that will dump into another CSV file.
Become a Member to join the conversation.