Community resourceWorksheet

1CP2-CT-10.3 From CSV file to structured solution

Part 3 of 5 · 1CP2-CT-10 · Integrated programming and data structures

The supplied clubs.csv uses the schema name,club,score, for example Asha,Robotics,68. Reading produces text. The loader must split each line, convert score to integer and append one typed row to a two-dimensional list.

Students will:

  • load and convert records from the supplied CSV file
  • decompose file, structure and processing stages
  • write a parameterised function over typed rows
  • format and test the required result

Inside: 5 explanation cells, 1 fill-in-the-blanks cell, 3 multiple-choice questions, 3 written answers and 1 Python task. 19 marks, about 45 minutes.

Series: 1CP2-CT-10 · Integrated programming and data structures, part 3 of 5.

Shared by Coding PathwayVerified teacher

  • 13 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.

From CSV file to structured solution

The supplied clubs.csv uses the schema name,club,score, for example Asha,Robotics,68. Reading produces text. The loader must split each line, convert score to integer and append one typed row to a two-dimensional list.

1. Decompose by data flow

File-to-structure program pipelineclubs.csvtext recordsload_recordssplit + convertreturn records2D listrows + fieldstyped valuessearchreturn recorddisplayformatted outputSmall interfaces expose data flow and make each stage easier to test.

load_records(filename) returns typed rows. find_record(records, target) returns a matched row or []. display_record(record) formats the chosen row. Parameters and returned values keep dependencies visible and reduce unnecessary globals.

Fill in the blanks3 marks
The loader receives a file gap 1 and returns a 2D gap 2. Score text is converted to gap 3.
  • integer
  • list
  • name
Multiple choice1 mark

Which design has the clearest data flow?

  • AEvery function changes a global records list
  • BThe loader returns records which are passed into the search
  • CThe display function secretly opens another file
  • DAll work is placed in one long main program

2. Predict a supplied function

def highest_score(records):
    highest = records[0][2]
    for row in records:
        if row[2] > highest:
            highest = row[2]
    return highest

print(highest_score([["A", "X", 6], ["B", "Y", 9]]))
Multiple choice1 mark

What exact value is printed?

  • A2
  • B6
  • C9
  • D15
Written answer2 marks

Explain why highest is initialised from the first record rather than zero.

Consider valid data that could be below zero.

Students type their answer here.

3. Complete the integrated stage

Starter code loads and converts clubs.csv. Define club_total(records, club_name) to return the total score for matching club rows. Store the result for Robotics in total and print exactly Robotics total: 149.

Coding task7 marks
records = []
club_file = open("clubs.csv", "r")
line = club_file.readline().strip("\n")
while line != "":
    fields = line.split(",")
    records.append([fields[0], fields[1], int(fields[2])])
    line = club_file.readline().strip("\n")
club_file.close()

# Define club_total, call it for Robotics and format the output.
Written answer2 marks

Give one test for a club present more than once and one test for an absent club, with expected totals.

Use the supplied file schema and exact numeric outcomes.

Students type their answer here.

Written answer2 marks

Explain one benefit of converting scores while loading instead of keeping them as strings.

Link type to later processing.

Students type their answer here.

Multiple choice1 mark

Why is club_file.close() included after reading?

  • ATo release the file resource after use
  • BTo delete the CSV
  • CTo sort records
  • DTo convert every field automatically

Route forward

You have built a file-to-typed-record pipeline and a focused processing function. Next you will use tests and trace evidence to repair and refine an integrated program.

Data files (1)
  • clubs.csv60 B