Practice Quiz: Reading & Writing CSV Files
Graded Quiz • 30 min Quiz5 Questions Week 2
Re⥃askly Logo

Practice Quiz: Reading & Writing CSV Files

Graded Quiz • 30 min Quiz5 Questions Week 2
Practice More Quizzes:
Quiz
Quiz
Quiz
5 Questions Week 1
Quiz
Quiz
5 Questions Week 4
Quiz
Quiz
5 Questions Week 5
Quiz
Quiz
5Questions Week 6
Quiz

Q:

We’re working with a list of flowers and some information about each one. The create_file function writes this information to a CSV file. The contents_of_file function reads this file into records and returns the information in a nicely formatted block. Fill in the gaps of the contents_of_file function to turn the data in the CSV file into a dictionary using DictReader. import osimport csv# Create a file with data in itdef create_file(filename): with open(filename, “w”) as file: file.write(“name,color,typen”) file.write(“carnation,pink,annualn”) file.write(“daffodil,yellow,perennialn”) file.write(“iris,blue,perennialn”) file.write(“poinsettia,red,perennialn”) file.write(“sunflower,yellow,annualn”)# Read the file contents and format the information about each rowdef contents_of_file(filename): return_string = “” # Call the function to create the file create_file(filename) # Open the file with open(filename) as file: # Read the rows of the file into a dictionary rows = csv.DictReader(file) # Process each item of the dictionary for row in rows: return_string += “a {} {} is {}n”.format(row[“color”], row[“name”], row[“type”]) return return_string#Call the functionprint(contents_of_file(“flowers.csv”))

Q:

Using the CSV file of flowers again, fill in the gaps of the contents_of_file function to process the data without turning it into a dictionary. How do you skip over the header record with the field names? import osimport csv# Create a file with data in itdef create_file(filename): with open(filename, “w”) as file: file.write(“name,color,typen”) file.write(“carnation,pink,annualn”) file.write(“daffodil,yellow,perennialn”) file.write(“iris,blue,perennialn”) file.write(“poinsettia,red,perennialn”) file.write(“sunflower,yellow,annualn”)# Read the file contents and format the information about each rowdef contents_of_file(filename): return_string = “” # Call the function to create the file create_file(filename) # Open the file with open(filename, ‘r’) as f: # Read the rows of the file rows = csv.reader(f) # Skips the headers next(rows) # Process each row for row in rows: name, color, typeflower = row # Format the return string for data rows only return_string += “a {} {} is {}n”.format(color, name, typeflower) return return_string#Call the functionprint(contents_of_file(“flowers.csv”))

Q:

Which of the following is true about unpacking values into variables when reading rows of a CSV file? (Check all that apply)

Q:

In order to use the writerows() function of DictWriter() to write a list of dictionaries to each line of a CSV file, what steps should we take? (Check all that apply)

Q:

If we are analyzing a file’s contents to correctly structure its data, what action are we performing on the file?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Find Questions in This Page: "CTRL+F"