CPT168 Ch7

¡Supera tus tareas y exámenes ahora con Quizwiz!

A binary file is like a text file in all but one of the following ways. Which one is it?

A binary file stores numbers with binary notation

import csv def main(): courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]] with open("courses.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerows(courses) course_list = [] with open("courses.csv", newline="") as file: reader = csv.reader(file) for row in reader: course_list.append(row) for i in range(len(course_list) - 2): course = course_list[i] print(course[0] + " (" + str(course[1]) + ")") main() Refer to Code Example 7-1. What will display on the console after the code executes?

Python (3) Trig (3)

import pickledef main():courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]]with open("classes.bin", "wb") as file: pickle.dump(courses, file)with open("classes.bin", "rb") as file: course_list = pickle.load(file)i = 0while i < len(course_list): course = course_list[i] print(course[0], str(course[1]), end=" ") i += 2main() Refer to Code Example 7-2: What is displayed on the console by the while loop?

Python 3 Physics 4

Which of the following is not true about a CSV File?

The csv module is a standard module so you don't need to import it

import csvdef main(): courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]]with open("courses.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerows(courses)course_list = []with open("courses.csv", newline="") as file: reader = csv.reader(file) for row in reader: course_list.append(row)for i in range(len(course_list) - 2): course = course_list[i] print(course[0] + " (" + str(course[1]) + ")") main() Refer to Code Example 7-1. If the first with open statement works, what is written to the file?

The list named courses

import pickle def main(): courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]]with open("classes.bin", "wb") as file: pickle.dump(courses, file) with open("classes.bin", "rb") as file: course_list = pickle.load(file) i = 0 while i < len(course_list): course = course_list[i] print(course[0], str(course[1]), end=" ") i += 2main() Refer to Code Example 7-2: What does the first with open statement do?

Writes the courses list to a binary file if the file named classes.bin doesn't exist

Which one of the following is not a benefit of using a with statement to open a file?

You don't have to specify the path for the file.

import csvdef main():courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]]with open("courses.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerows(courses)course_list = []with open("courses.csv", newline="") as file: reader = csv.reader(file) for row in reader: course_list.append(row)for i in range(len(course_list) - 2): course = course_list[i] print(course[0] + " (" + str(course[1]) + ")") main() Refer to Code Example 7-1. What happens if the courses.csv file doesn't exist when the first with open statement is executed?

a new file named courses.csv is created

import pickledef main():courses = [["Python", 3], ["Trig", 3], ["Physics", 4], ["Yoga", 2]]with open("classes.bin", "wb") as file: pickle.dump(courses, file)with open("classes.bin", "rb") as file: course_list = pickle.load(file)i = 0while i < len(course_list): course = course_list[i] print(course[0], str(course[1]), end=" ") i += 2main() Refer to Code Example 7-2: What does the second with open statement do?

causes an exception if the file named classes.bin doesn't exist

To work with a file when you're using Python, you must do all but one of the following. Which one is it?

decode the data in the file

To read the rows in a CSV file, you need to

get a reader object by using the reader() fuunction of the csv module

To read a list of lists that's stored in a binary file, you use

the load() method of the pickle module

Given the following 2-dimensional list of 3 rows and 3 columns, how would you write this list to a CSV file named prog.csv?programming = [["Python", "cop1000", 3], ["Java", "cop1020", 3], ["HTML5", "cop1040", 3]]

with open("prog.csv", "w", newline = "") as file: writer = csv.writer(file) writer.writerows(programming)


Conjuntos de estudio relacionados

[G11 STEM] Oral Communication L3: Verbal and Non-verbal Communication

View Set

Chapter 4: Printers and Multi-function Devices

View Set

RN Pharmacology Online Practice B

View Set

Vocabulary Power Plus Level 8 LESSON 4

View Set

Chapter 2: Types of Life Insurance Policies

View Set