Dictionaries and Files

The questions below are due on Thursday June 24, 2021; 11:00:00 AM.
 
You are not logged in.

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.
Background

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!).

Try Now:

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.

Try the following line: print(row['ITEM'], row['UNITS']).1

The Task

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.

grocery_list.csv prices.csv
Given the data we have seen so far, linked again above, how much was the grocery bill?

grocery_list_large.csv
How much is the grocery bill for this larger grocery list, using the same prices?

Note

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.

Try Now:

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:

  • Allowing only the grocery items containing the letter 'b' to contribute to the final grocery bill.
  • Given a list of taxable items in a separate csv file, applying a 6% tax to those items.
  • Due to a store promotion, making every 100th item free.
    • 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 whether grocery_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 ifto check whether the current item is a hundredth item (e.g. if item_count%100 == 0).
  • Next Exercise: Comparisons

    Back to exercises


     
    Footnotes

    1Actually, depending on your version of Python, it may actually be an OrderedDict, a type that acts very much like a normal dictionary, but which maintains an order on the key-value pairs. You can treat it like a dictionary regardless (click to return to text)