Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1next(reader) | ARemoves the newline, splits the line at each comma and adds the fields to the list as one record | |
| 2records.append(line.strip().split(',')) | BThe whole line is appended as one long string. It is never split into fields, so the list is not a table of records. | |
| 3Each field is read from a file as a string, so a numeric field must be converted before it is used in calculations | CSkips the header row by reading and discarding the first line before the loop reads the data rows | |
| 4The csv module reader splits every line into a list of fields for you, so you can loop over the rows directly | Drecord[2] = int(record[2]) | |
| 5records = [] for line in file: records.append(line) | EThe header row is not skipped, so the column names are stored as a record and mixed in with the real data. | |
| 6for line in file: records.append(line.strip().split(',')) # header row is included | Ffor row in csv.reader(file): |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
records.append(line.strip().split(',')) — Splits one CSV line into fields and appends them as one record to a list, so each row of the file becomes a record and the whole file becomes a table in memory.for line in file:
records.append(line.strip().split(','))file = open('students.csv')
next(file)
for line in file:parts = line.strip().split(',')
records.append([parts[0], int(parts[1])])import csv
reader = csv.reader(file)
records = list(reader)records = []
for line in file:
records.append(line.strip().split(','))