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 | |
|---|---|---|
| 1line.strip().split(',') | Amark = float(fields[2]) | |
| 2parts = 'Ava,7,88.5'.split(',') | Bfields = line.split('\t') | |
| 3Choose the delimiter that the file actually uses, such as a tab, by passing it to split() | CCreates the list ['Ava', '7', '88.5'] where every field is still a string | |
| 4Convert a field to a number after splitting, because split() gives strings | DRemoves the newline from the end of the line, then breaks it into a list of field strings at each comma | |
| 5fields = line.split(',') name = fields[0] total = fields[1] + 5 | EThe field fields[1] is still a string, so adding the number 5 causes a type error. Convert it first with int(fields[1]) + 5. | |
| 6fields = line.split(';') # CSV uses commas | FThe wrong delimiter is used, so the line is not split and the list has only one item. Use line.split(',') to match the commas. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
line.split(',') — A string method that breaks a line of text into a list of field strings at each comma. Use strip() first to remove the newline, and int() or float() to convert fields.line = "Ava,88,74"
fields = line.split(',')
print(fields[0])line = "Ava,88,74\n"
clean = line.strip()
fields = clean.split(',')line = "Liam;65;70"
DELIM = ';'
fields = line.split(DELIM)fields = line.strip().split(',')
mark = int(fields[1])
print(mark + 5)line = "Pencil,Stationery,1.5"
fields = line.split(',')
price = float(fields[2])