8.17. Group Work: Using a CSV reader/writer

It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.

Note

If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.

Learning Objectives

Students will know and be able to do the following.

Content Objectives:

Process Objectives:

8.17.1. CSV Reader

Many people need to read and process CSV files. There is a module that makes this process easier.

Look at the data in the file below.

We can write Python code to read the data using a csv reader and find the date with the highest value at the close.

Run the code below to find the date with the highest value at the close.

8.17.2. Reading Comma-Separated Values (CSV) Files with a Header Row

Here is another sample example CSV file. It contains the number of passengers (in thousands) for transatlantic air travel for each month for the years 1958 to 1960. The first row is a header that explains the data. The data is from https://people.sc.fsu.edu/~jburkardt/data/csv/csv.html.

We can read the data from the file and store it in a nested dictionary. In this case the outer dictionary will use the month as the key and the inner dictionary will use the years as the keys. It will use the data from the header row for the year keys.

Run the code below. It is supposed to print the nested dictionary and then the total number of passengers (in thousands) for 1958, but there are errors. Fix the errors so that all tests pass.

Fix the code below to work correctly. It should print the month with the highest number of passengers in 1958.

8.17.3. Writing a Comma-Separated Values (CSV) File with CSV Writer

Here is another sample example CSV file. It contains the Oscar winners for Best Actress from 1928 to 2016. It has a header row to explain the data in each column.

We can read the data from the file and store it in a list of dictionaires where the keys in the dictionary are ‘year’, ‘age’, ‘name’, and ‘movie’.

Fix the code below. It should read all the data into a list of dictionaries. Then it should create a new dictionary where the key is the age and the value is the number of actresses who won at that age. It should sort the items in the dictionary by the number of winners descending and return the top five tuples. Then it will write the data to a csv file using a csv writer.

You can read more about the CSV module at https://docs.python.org/3/library/csv.html.

If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.

The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.

You have attempted of activities on this page