Learning Objectives • Open a file for writing Process information in a dictionary • Practice writing code needed for the final project Instructions A dictionary is a great way to store information in the real world and it is time for us to try it out on files! If you will continue to learn python or programming you will learn about the JSON encryption and "json" module in python, but for now, let's save the dictionary in CSV type that we already learned. If the format of the dictionary looks like this: { id: [name, prct, grades_list] } e.g. { 101: ["quiz", 12, [1,2,3,4]], 102: ["final", 53, [10, 10, 10, 10]] } The file contents should look like 101,quiz,12,1,2,3,4 102,final,53,10,10,10,10 Notice that the list became "unwrapped". It is ok since we know that the 3 first elements in line are if, name, and percentage (prct), and all the rest is the grades_list. 1. Create a function save_dict_to_csv(storage, filename) that will accept the dictionary and the string with future filename 1.0. Use with to open the file with the required filename. Please, use open exactly like this: with open(filename, newline=''). Please notice newline parameter here - it will prevent csv library to write extra empty lines in the file. Check the example in your IDE with and without newline="" and find the difference. 1.1. Use a loop to loop over all keys (and values if you need them) in the dictionary 1.2. Using knowledge about the structure of the dictionary prepare the line to be saved (key, first elem, second elem, third elem)

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

I have the following code but it is not running correctly!

 

import csv
lst = []
def save_dict_to_csv(storage, file_name):
    with open(file_name, 'w', newline = '') as f:
        f.write(file_name)
        f.write('\n')
        for key, value in storage.items():
            lst1 = []
            lst1.append(str(key))
            for val in value :
                if type(val) == list:
                    for v in val:
                        lst1.append(str(v))
                
                else:
                    lst1.append(str(val))
            for i in range(len(lst1)):
                if i != (len(lst1) - 1):
                    f.writelines(lst1[i] + ',')
                else:
                    f.writelines(lst1[i])
            f.writelines('\n')
    f = open(file_name)
    print(f.read())
                    
                
                

if __name__ == "__main__":
    storage = { 101: ["quiz", 12, [1,2,3,4]],
    102: ["final", 53, [10, 10, 10, 10]] }
    res = save_dict_to_csv(storage, "storage_save.txt")
    print(res)

 

The file contents should look like
101,quiz,12,1,2,3,4
102,final,53,10,10,10, 10
Notice that the list became "unwrapped". It is ok since we know that the 3 first elements in line are if, name, and percentage (prct), and all
the rest is the grades_list.
1. Create a function save_dict_to_csv(storage, filename) that will accept the dictionary and the string with future filename
1.0. Use with to open the file with the required filename. Please, use open exactly like this: with open(filename, 'w',
newline=''). Please notice newline parameter here - it will prevent csv library to write extra empty lines in the file. Check the
example in your IDE with and without newline="" and find the difference.
1.1. Use a loop to loop over all keys (and values if you need them) in the dictionary
1.2. Using knowledge about the structure of the dictionary prepare the line to be saved (key, first elem, second elem, third elem)
1.3. Use csv module and writer in it to save all lines.
2. You do not have to have any if
main
statement, but you can use it and IDE on your computer to test your
name
program.
Examples of function work:
storage =
{ 101: ["quiz", 12, [1, 2,3,4]],
102: ["final", 53, [10, 10, 10, 10]] }
save_dict_to_csv(storage, "storage_save.txt")
storage save.txt
101,"quiz", 12,1,2,3,4
102,"final",53,10, 10, 10, 10
Transcribed Image Text:The file contents should look like 101,quiz,12,1,2,3,4 102,final,53,10,10,10, 10 Notice that the list became "unwrapped". It is ok since we know that the 3 first elements in line are if, name, and percentage (prct), and all the rest is the grades_list. 1. Create a function save_dict_to_csv(storage, filename) that will accept the dictionary and the string with future filename 1.0. Use with to open the file with the required filename. Please, use open exactly like this: with open(filename, 'w', newline=''). Please notice newline parameter here - it will prevent csv library to write extra empty lines in the file. Check the example in your IDE with and without newline="" and find the difference. 1.1. Use a loop to loop over all keys (and values if you need them) in the dictionary 1.2. Using knowledge about the structure of the dictionary prepare the line to be saved (key, first elem, second elem, third elem) 1.3. Use csv module and writer in it to save all lines. 2. You do not have to have any if main statement, but you can use it and IDE on your computer to test your name program. Examples of function work: storage = { 101: ["quiz", 12, [1, 2,3,4]], 102: ["final", 53, [10, 10, 10, 10]] } save_dict_to_csv(storage, "storage_save.txt") storage save.txt 101,"quiz", 12,1,2,3,4 102,"final",53,10, 10, 10, 10
Learning Objectives
Open a file for writing
Process information in a dictionary
Practice writing code needed for the final project
Instructions
A dictionary is a great way to store information in the real world and it is time for us to try it out on files! If you will continue to learn python
or programming you will learn about the JSON encryption and "json" module in python, but for now, let's save the dictionary in CSV type that
we already learned.
If the format of the dictionary looks like this: { id: [name, prct, grades_list] }e.g.
{
101: ["quiz", 12, [1,2,3,4]],
102: ["final", 53, [10, 10, 10, 10]]
}
The file contents should look like
101,quiz,12,1,2,3,4
102,final,53,10,10,10, 10
Notice that the list became "unwrapped". It is ok since we know that the 3 first elements in line are if, name, and percentage (prct), and all
the rest is the grades_list.
1. Create a function save_dict_to_csv(storage, filename) that will accept the dictionary and the string with future filename
1.0. Use with to open the file with the required filename. Please, use open exactly like this: with open(filename, 'w',
newline=''). Please notice newline parameter here - it will prevent csv library to write extra empty lines in the file. Check the
example in your IDE with and without newline="" and find the difference.
1.1. Use a loop to loop over all keys (and values if you need them) in the dictionary
1.2. Using knowledge about the structure of the dictionary prepare the line to be saved (key, first elem, second elem, third elem)
Transcribed Image Text:Learning Objectives Open a file for writing Process information in a dictionary Practice writing code needed for the final project Instructions A dictionary is a great way to store information in the real world and it is time for us to try it out on files! If you will continue to learn python or programming you will learn about the JSON encryption and "json" module in python, but for now, let's save the dictionary in CSV type that we already learned. If the format of the dictionary looks like this: { id: [name, prct, grades_list] }e.g. { 101: ["quiz", 12, [1,2,3,4]], 102: ["final", 53, [10, 10, 10, 10]] } The file contents should look like 101,quiz,12,1,2,3,4 102,final,53,10,10,10, 10 Notice that the list became "unwrapped". It is ok since we know that the 3 first elements in line are if, name, and percentage (prct), and all the rest is the grades_list. 1. Create a function save_dict_to_csv(storage, filename) that will accept the dictionary and the string with future filename 1.0. Use with to open the file with the required filename. Please, use open exactly like this: with open(filename, 'w', newline=''). Please notice newline parameter here - it will prevent csv library to write extra empty lines in the file. Check the example in your IDE with and without newline="" and find the difference. 1.1. Use a loop to loop over all keys (and values if you need them) in the dictionary 1.2. Using knowledge about the structure of the dictionary prepare the line to be saved (key, first elem, second elem, third elem)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Header Files
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
  • SEE MORE 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