Peter,1000
Joan,50500 Mary,2400
Module 9 Assignments
Programming Problems
Programming Problems should be submitted to gradescope.
Programming Problem 17
Due date: Oct 29, Tuesday at 7pm
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: Oct 29, Tuesday at 7pm
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.