Community resourceWorksheet

1CP2-CT-5.4 Processing CSV records

Part 4 of 7 · 1CP2-CT-5 · Merge sort, files and authentication

The record processing worksheet, splitting a line into fields and converting the ones that must be numbers.

Students will:

  • split a record and read the field they need
  • explain the error that removing a conversion would cause
  • trace a loop that totals a numeric field across records
  • process several records into a new structure
  • describe checks that protect a program's assumptions about a file

Inside: 6 explanation cells, 3 multiple-choice questions, 1 Python task, 1 trace table, 1 fill-in-the-blanks cell and 2 written answers. 15 marks, about 45 minutes.

Series: 1CP2-CT-5 · Merge sort, files and authentication, part 4 of 7.

Shared by Coding PathwayVerified teacher

  • 14 cells
  • About 45 minutes
  • CC BY-SA 4.0
  • Shared 17 Aug 2026

Preview

The whole resource, exactly as a class sees it. Answers and marking are held back.

Processing CSV records

A line read from a CSV file is one string. Programs normally remove the line ending, split the string at commas, then convert fields to the data types required for processing.

1. Transform one record

Reading and processing a CSV text fileopen "r"connect to fileread lineone recordstripremove split(",")make fieldsconvert / usethen close fileCSV text becomes usable program data in stagessplit produces strings: convert numeric fields before arithmetic.

For "Chess,18\n", strip() produces "Chess,18"; split(",") produces ["Chess", "18"]; int(fields[1]) produces the integer 18. Splitting does not infer types: every field initially remains text.

Fill in the blanks3 marks
strip() removes surrounding whitespace such as gap 1. split(",") creates a gap 2 of fields. A numeric field must be gap 3 before arithmetic.
  • converted
  • list
  • newlines
  • sorted

2. Predict the transformation

line = "Robotics,12\n"
fields = line.strip("\n").split(",")
places = int(fields[1]) + 3
print("{} {}".format(fields[0], places))
Multiple choice1 mark

What is printed?

  • A`Robotics 123`
  • B`Robotics 15`
  • C`12 3`
  • D`Robotics,12 15`
Written answer2 marks

Explain what error would occur if int() were removed and why.

Compare adding an integer with using a string field.

Students type their answer here.

Trace table5 marks

Trace the loop that totals the numeric field of each already-split record.

Record changes to `record`, `places` and `total` for each iteration. The split and conversion stages have already produced typed rows.

Use one row for each pass through the loop. Fill in a box only when that value changes on that row, and leave the rest blank.

ProgramPython
  1. records = [["Chess", 18], ["Robotics", 12], ["Coding", 21]]
  2. total = 0
  3. for record in records:
  4. places = record[1]
  5. total = total + places
  6. print(total)
Trace table with 5 columns
RowrecordstotalrecordplacesOutput
1
2
3
4
5
6
7

3. Process several records

The supplied list represents lines already read from a CSV file. Build a rectangular two-dimensional list named clubs. Each row must contain the club name as text and places as an integer. Also calculate total_places and print it.

Coding task5 marks
file_lines = ["Chess,18\n", "Robotics,12\n", "Coding,21\n"]
clubs = []
total_places = 0
# Process every line into [name, integer places].

4. Protect assumptions

Core examination data may be well formed, but robust design identifies assumptions: each row should contain the expected number of fields, required fields should be present, and numeric text should be suitable for conversion. Validation can occur before a record is used.

Multiple choice1 mark

Which line would fail at int(fields[1])?

  • A`Chess,18`
  • B`Coding,21`
  • C`Robotics,twelve`
  • D`Art,0`
Written answer2 marks

Describe two checks a program could make before accepting a club record from a file.

Use the expected fields and their contents.

Students type their answer here.

Multiple choice1 mark

Why build [name, places] instead of keeping the original line?

  • ASo the comma is encrypted.
  • BSo fields can be indexed separately and the number can use an appropriate type.
  • CSo the file is automatically appended.
  • DSo sorting is no longer possible.

Route forward

You can transform CSV text into structured, typed program data. Next you will create and update CSV files using write and append modes.