We are going to take a dictionary that has a Month (str) key and 2 integer Temps (as a list) value. For every entry in the dictionary, we will create another list of the [Month, Temp1, Temp2] and write that (i.e. save it) in a CSV file.
We are going to take a dictionary that has a Month (str) key and 2 integer Temps (as a list) value. For every entry in the dictionary, we will create another list of the [Month, Temp1, Temp2] and write that (i.e. save it) in a CSV file.
We are going to take a dictionary that has a Month (str) key and 2 integer Temps (as a list) value. For every entry in the dictionary, we will create another list of the [Month, Temp1, Temp2] and write that (i.e. save it) in a CSV file.
def save_temperature_statistics(input_dict, filename): """ We are going to take a dictionary that has a Month (str) key and 2 integer Temps (as a list) value. For every entry in the dictionary, we will create another list of the [Month, Temp1, Temp2] and write that (i.e. save it) in a CSV file. """
Transcribed Image Text:Introduction
Your success with Anita's task now has Chen asking for your help: can you store the data from a Python dictionary into a csv file?
Instructions
Given a dictionary with the month as a key and the temperatures stored in a list, you need to write this dictionary into the csv file
fahrenheit_data.csv which will look like
January, 67,47
February, 67,47
Steps
1. Create a function save_temperature_statistics (input_dict, filename) that will accept a dictionary with the month-
temperature mappings and a string with the filename to which to save the data.
1.1 Use the with construct to open the file with the required filename. You will need open the file for writing (the w flag) exactly like
this:
with open (filename, 'w', newline='') as csvfile
Please notice the newline parameter here - it will prevent the csv library from writing extra empty lines in the file. Check the example
in your IDE with and without the newline="" to see the difference.
1.2 After opening the file, create the csv.writer object as csv_writer = csv.writer (csvfile).
1.3 After defining csv_writer, loop over all items in the dictionary using a for loop.
1.4.1. For each key and value pair in the dictionary (input_dict), create a new list temperature_info_list that contains 3
elements. The first element is the key of the current pair. The next two elements are the two elements in the value list. For instance,
temperature_info_list for the item with key as "January" and value as [67, 47] will be temperature_info_list =
["January", 67, 47].
1.4.2. Save this list as a line in the file using the csv_writer.writerow(temperature_info_list). The function writerow()
writes this information into the file by separating the list elements by commas.
2. In your main program, i.e, in the if name == _main__" block, you should create temperature_dict and store the previous
weather information provided to Anita. You should then call the function save_temperature_statistics (temperature_dict,
'fahrenheit_data.csv') to write the dictionary to the csv file.
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.