Edexcel GCSE Computer Science June 2022 Paper 2, Question 5: writing a CSV file
Pearson Edexcel 1CP2/02 June 2022, Question 5: write a Python program that saves a one-dimensional list of coffee weights to a comma-separated text file with seven weights on each line. A model answer, the fifteen marks explained, and the program to run.
Question 5 of the Pearson Edexcel GCSE Computer Science Paper 2 from the June 2022 series (1CP2/02) is a 15 mark program you write yourself. A flat list of numbers has to come out as a text file with seven values on each line, separated by commas.
We do not copy the exam paper or Pearson's code files here. Open the paper beside this page: Edexcel June 2022 Paper 2 question paper (PDF). When you have finished, check the mark scheme too.
The question in short
A cafe records the kilograms of coffee it uses each day in a one-dimensional list: 63 numbers, which is nine weeks. Write them to a comma-separated file called Q05_OUTPUT.TXT. Each line of the file is one week: seven weights, with commas between them and no comma at the end. Process every weight in the list.
The two things to solve
When does a line end? Keep a count of how many weights are on the current line. When it reaches 7, write a line feed and set the count back to 0.
Where do the commas go? Between values, not after the last one on a line. So each value is followed by a comma or by a line feed, never both.
A model answer
OUTPUT_FILE = "Q05_OUTPUT.TXT"
PER_LINE = 7
outFile = open(OUTPUT_FILE, "w")
count = 0
for weight in coffeeWeights:
count = count + 1
if count == PER_LINE:
outFile.write(str(weight) + "\n")
count = 0
else:
outFile.write(str(weight) + ",")
outFile.close()
Where the fifteen marks are
Nine single marks: the file opened for writing only; a constant used for the file name; a loop over every item; a mechanism that controls seven items a line; a constant compared against the count; a line feed on each line; commas on every item except the last of a line; the file closed; and readable code.
Then up to 3 marks for the design of the solution and up to 3 for how well it runs. A for loop is preferred to a while for going through a whole list, and the numbers must be converted to strings with str().
Where the marks are lost
"a"for the file mode. Append adds to the old file every time you run it, and you will run it several times. Use"w".- A comma at the end of every line. Compare your file with the expected output in the paper, character by character.
- Writing the number itself.
write()only takes strings.outFile.write(weight)is a TypeError. - 7 typed into the
if. Two of the marks are for using constants. - Not closing the file. Until it is closed, the last lines may not be saved.
- Assuming 63 items. Loop over the list, whatever its length.
Run it
This version uses three weeks of the data, writes the file, then reads it back so that you can see it. Change PER_LINE to 3 and look at the difference.
The program
from bugbot import *
connect()
OUTPUT_FILE = "Q05_OUTPUT.TXT"
PER_LINE = 7
coffeeWeights = [3.79, 4.16, 1.52, 3.66, 2.58, 4.98, 4.37,
2.95, 2.58, 4.37, 4.59, 2.61, 6.13, 4.49,
1.66, 2.65, 4.64, 4.72, 3.59, 4.56, 4.23]
outFile = open(OUTPUT_FILE, "w")
count = 0
for weight in coffeeWeights:
count = count + 1
if count == PER_LINE:
outFile.write(str(weight) + "\n")
count = 0
else:
outFile.write(str(weight) + ",")
outFile.close()
inFile = open(OUTPUT_FILE, "r")
print(inFile.read())
inFile.close()
Questions
What is the answer to Edexcel GCSE Computer Science 2022 Paper 2 Question 5?
Open the output file for writing using a constant for its name. Loop over every weight, counting as you go. Write each weight as a string followed by a comma, except every seventh, which is followed by a line feed and resets the count. Close the file.
How do I write a number to a text file in Python?
Convert it to a string first: outFile.write(str(number)). Add "\n" to end the line.
What is the difference between "w" and "a" when opening a file?
"w" creates the file, or empties it if it exists, and writes from the start. "a" keeps what is there and adds to the end.
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.4 Iterating over a list Strings, lists and records
- F13.4 Programming questions Exam preparation
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.