Peter,1000
Joan,50500
Mary,2400Module 9 Assignments
Programming Problems
Programming Problems should be submitted to gradescope.
Programming Problem 17
Due date: Thursday, March 28, 2024 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_namein 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.74print( 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: Thursday, March 28, 2024 at 9pm
Write a Python function that does the following:
- Its name is
write_csv - It takes a
dictionaryand a stringfile_nameas arguments - It opens the file with
file_namein write mode - It iterates over the
dictionaryto 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:
my_data = {"Peter": [1000], "Joan": [50500], "Mary": [2400]}
write_csv(my_data, "stipends.csv")This is what stipends.csv should contain:
Peter,1000
Joan,50500
Mary,2400my_data = {"Country": ["United States", "Brazil", "Mexico", "Canada"],
"Population (in mil)": [331.00, 212.56, 128.93, 37.74]}
write_csv(my_data, "population.csv")This is what population.csv should contain:
Country,United States,Brazil,Mexico,Canada
Population (in mil),331.0,212.56,128.93,37.74Name the program write_data_file.py. Make sure that gradescope gives you the points for passing the test case.