Peter,1000
Joan,50500 Mary,2400
Module 9 Assignments
Short Project 07
Short Programming projects are submitted during our weekly 45-minute in-person lab sessions. Each lab sessions is guided by two TAs. The instructions for the short project will be available only during the lab sessions. To schedule your lab session go to the weekly lab session spreadsheet in Short Project Dates and Instructions.
Programming Problems
Programming Problems should be submitted to gradescope.
Programming Problem 17
Due date: Friday, March 28, 2025 at 9pm
Write a Python function that does the following:
- Its name is
read_csv
- It takes a string as argument called
file_name
- It opens the file with
file_name
in read mode - It iterates over each line in the file, splitting each line by
","
- It creates a dictionary where the keys are the first item in each line, and the values are the other values (in a list)
- It returns the dictionary
Test cases:
contents of stipends.csv:
print( read_csv("stipends.csv") ) # {"Peter": [1000],
# "Joan": [50500],
# "Mary": [2400]}
contents of population.csv:
Country,United States,Brazil,Mexico,Canada Population (in mil),331.00,212.56,128.93,37.74
print( read_csv("population.csv") ) # {"Country": ["United States", "Brazil", "Mexico", "Canada"],
# "Population (in mil)": [331.00, 212.56, 128.93, 37.74]}
Name the program read_data_file.py
. Make sure that gradescope gives you the points for passing the test case.
Programming Problem 18
Due date: Friday, March 28, 2025 at 9pm
Write a Python function that does the following:
- Its name is
write_csv
- It takes a
dictionary
and a stringfile_name
as arguments - It opens the file with
file_name
in write mode - It iterates over the
dictionary
to write lines to the opened.csv
- Each key is the first element of the line, the values are lists that contain the other values in the line
Test cases:
= {"Peter": [1000], "Joan": [50500], "Mary": [2400]}
my_data "stipends.csv") write_csv(my_data,
This is what stipends.csv should contain:
Peter,1000
Joan,50500 Mary,2400
= {"Country": ["United States", "Brazil", "Mexico", "Canada"],
my_data "Population (in mil)": [331.00, 212.56, 128.93, 37.74]}
"population.csv") write_csv(my_data,
This is what population.csv should contain:
Country,United States,Brazil,Mexico,Canada Population (in mil),331.0,212.56,128.93,37.74
Name the program write_data_file.py
. Make sure that gradescope gives you the points for passing the test case.