Dictionaries and Files
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Relevant Reading about interacting with csv files that is useful to read/work through before this exercise.
This exercise will introduce one more tool for reading files. We'll use the open
function and the csv
module from the readings. This time, we'll read in CSV files with column titles into dictionaries. We'll use the same scenario as before, grocery lists. This time the list has a number next to each item, which indicates how many units of it should be bought.
Here's what the data looks like: grocery_list.csv
Recall the code we created to read from a file before:
import csv
with open("grocery_list.csv", "r") as opened_file:
reader = csv.reader(opened_file)
for row in reader:
print(row)
Now our grocery file has two columns, with titles ITEM
and UNITS
. We can use the csv module's DictReader
to access these values:
import csv
with open("grocery_list.csv", "r") as opened_file:
dict_reader = csv.DictReader(opened_file) # different
for row in dict_reader:
pass # we'll fill in later
This time, row
is not a string, but rather a dictionary1 mapping the column titles (strings) to their values (also strings!).
Print out each item name and its units by changing the line of code which is currently pass
. You can download the starter code from above here. Be sure to store this file in the same directory as grocery_list.csv.
For this exercise, you'll also need another file listing items and their corresponding price per unit: prices.csv.
Write code which, given the two files like described, computes the total grocery bill. Use your code to answer the questions below. Note that you don't necessarily need to use dictionaries for this problem, but we'd recommend it.
You may assume that the items in the grocery list file are also in the prices file, in the exact same string format. You may also assume that the column titles will never change--'ITEM'
and 'UNITS'
in the grocery list file, and 'ITEM'
and 'PRICE PER UNIT'
in the prices file.
As always, you should first make a plan. How will you store the data that you're reading in? It's also a great idea to make a shorter grocery list to test your code incrementally.
Given the data we have seen so far, linked again above, how much was the grocery bill?
How much is the grocery bill for this larger grocery list, using the same prices?
If you are familiar enough with Microsoft Excel that you would have been able to do this exercise entirely there, then in the real world, it might admittedly be overkill for you to pull out Python for this. That said, Python is in general a more powerful tool which will enable you to do things that would be too complex for Excel.
Here are some customizations of this problem which, given your current Python knowledge, you would already be able to implement, and which might be difficult in Excel. Think generally about how you would implement each of these in your code:
'b'
to contribute to the final grocery bill.
- This likely involves the addition of a single statement within your loop:
if 'b' in grocery_item:
- This again entails the addition of an
if
, this time checking whethergrocery_item in taxable_items
, and, if so, adding 6% of that item's price to the total.taxable_items
can be constructed by reading the csv file. - This could be achieved by keeping a counter of the item number as you iterate through the items, and adding another
if
to check whether the current item is a hundredth item (e.g.if item_count%100 == 0
).
Next Exercise: Comparisons