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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

import csv

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.    
    """


if __name__ == '__main__':
    temperature_dict = {
        "January": [67,47],
        "February": [67,47],
        "March": [68,49],
        "April": [71, 51],
        "May": [72, 54],
        "June": [73, 57],
        "July": [76, 60],
        "August": [78, 60],
        "September": [78, 60],
        "October": [76, 56],
        "November":[71, 50],
        "December": [66, 46]
    }
    save_temperature_statistics(temperature_dict, 'fahrenheit_data.csv')    

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.
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Lists
Learn more about
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.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education