Edexcel GCSE Computer Science June 2024 Paper 2, Question 6: cows from a CSV file
Pearson Edexcel 1CP2/02 June 2024, Question 6: write a Python program that reads a comma-separated file of cows, builds a key from the breed, the tag number and the name, stores a record for each cow in a 2D list and displays the table. A model answer and the fifteen marks.
Question 6 is the last question of the Pearson Edexcel GCSE Computer Science Paper 2 sat on 21 May 2024 (1CP2/02): 15 marks for reading a text file, turning each line into a record and building a table. It is the standard file-to-table pattern, plus some string slicing to make a key.
We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2024 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
A file called Cows.txt has one cow per line: name, breed and tag number, separated by commas.
For each cow, make a key: the first two letters of the breed, then the tag number integer-divided by 100, then the first two letters of the name. So Annabelle, Highland, 2569 has the key Hi25An.
Make a record for each cow in this order: key, tag number, name, breed. Store the records in cowTable. Call the supplied showTable() to display it. It must work for any number of lines.
The four steps for each line
- Strip the line feed off the end. Otherwise it stays on the last field.
- Split the line on the commas into a list of fields.
- Build the key from slices:
breed[0:2],int(tag) // 100,name[0:2]. - Append the record, as a list in the right order, to the table.
A model answer
INPUT_FILE = "Cows.txt"
cowTable = []
inFile = open(INPUT_FILE, "r")
for line in inFile:
line = line.strip()
fields = line.split(",")
name = fields[0]
breed = fields[1]
tag = int(fields[2])
key = breed[0:2] + str(tag // 100) + name[0:2]
record = [key, tag, name, breed]
cowTable.append(record)
inFile.close()
showTable(cowTable)
Where the fifteen marks are
Six single marks: every line of the file processed; the line feed stripped; the line split on commas; the first two characters of the breed or name extracted; the number part worked out with // 100; and showTable() called correctly with the table.
Then up to 3 each for design (the file opened and closed, iteration used well, sensible data structures), good practice (layout, names, comments) and functionality (the fields in the right order in every record).
Where the marks are lost
- Not stripping. The breed of the last field would be "Highland\n", and the key would still look right, so it is easy to miss. Print a record and look for the stray line.
/ 100for// 100. 25.69 is not 25. The key needs integer division.- The record in file order. The file is name, breed, tag. The record is key, tag, name, breed.
- A record that is one string.
"Hi25An,2569,Annabelle,Highland"is not a record. It has to be a list of four items. - Assuming six cows. A
forloop over the file handles any number.
Run it
This version writes a small Cows.txt first, since the exam gives you the file, then reads it back the way the answer does. showTable() is written out, as it would be in the given file.
The program
from bugbot import *
connect()
# in the exam this file is given to you
outFile = open("Cows.txt", "w")
outFile.write("Annabelle,Highland,2569\nBonnie,Shetland,3798\nCarmine,Belted Galloway,4521\n")
outFile.write("Delores,Highland,5736\nEvette,Shetland,6504\nFrancis,Belted Galloway,7713\n")
outFile.close()
def showTable(table):
for record in table:
print(record)
INPUT_FILE = "Cows.txt"
cowTable = []
inFile = open(INPUT_FILE, "r")
for line in inFile:
line = line.strip()
fields = line.split(",")
name = fields[0]
breed = fields[1]
tag = int(fields[2])
# key: two letters of breed, tag DIV 100, two letters of name
key = breed[0:2] + str(tag // 100) + name[0:2]
record = [key, tag, name, breed]
cowTable.append(record)
inFile.close()
showTable(cowTable)
led("green")
Questions
How do I read a CSV file into a list of records in Python?
Open the file, loop over its lines, strip each line and split it on commas, build a list of the fields in the order you want, and append that list to the table. Close the file afterwards.
What does split() do?
It cuts a string into a list wherever a separator appears. "a,b,c".split(",") gives ["a", "b", "c"].
Why strip the line before splitting it?
Each line read from a file ends with a line feed character. Without strip(), the last field keeps that character, and comparisons or output involving it go wrong.
More from this paper
Every Edexcel 1CP2 question we have worked
Learn it step by step
- F7.1 Reading and writing files Files and databases
- F3.6 Records Strings, lists and records
- F3.1 String handling Strings, lists and records
This is our own explanation of a published exam question. It is not written or endorsed by Pearson, and the question paper and mark scheme remain Pearson's copyright. Read them on Pearson's site with the links on this page.