Session Ready
Exercise

Creating a dictionary from a file

The csv module also provides a way to directly create a dictionary from a CSV file with the DictReader class. If the file has a header row, that row will automatically be used as the keys for the dictionary. However, if not, you can supply a list of keys to be used. Each row from the file is returned as a dictionary. Using DictReader can make it much easier to read your code and understand what data is being used, especially when compared to the numbered indexes you used in the prior exercise.

Your job in this exercise is to create a dictionary directly from the data file using DictReader. NOTE: The misspellings are from the original data, and this is a very common issue. Again, the baby_names dictionary has already been created for you.

Instructions
100 XP
  • Import the Python csv module.
  • Create a Python file object in read mode for the baby_names.csv called csvfile.
  • Loop over a csv DictReader on csvfile. Inside the loop:
    • Print each row.
    • Add the 'RANK' of each row as the key and 'NAME' of each row as the value to the existing dictionary.
  • Print the dictionary keys.