How do you accept user input and read from and write to comma separated value (CSV) text files in Python?
Write programs that accept and respond appropriately to user input, and that read from and write to comma separated value text files.
A focused answer to Edexcel GCSE Computer Science 6.4.1 and 6.4.2, covering accepting and responding to user input, and reading from and writing to comma separated value (CSV) text files in Python.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
Edexcel wants you to write programs that accept and respond to user input, and that read from and write to comma separated value (CSV) text files in Python, which is a core skill for the practical Paper 2.
Accepting and responding to user input
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello " + name + ", next year you will be " + str(age + 1))
The conversion is essential: input() gives text, so age + 1 would fail or concatenate wrongly without int(). Note also that to join a number into output you convert it back with str(). Responding appropriately means doing something sensible with the input, such as checking it (validation, covered separately) or using it in a calculation.
Reading from a text file
total = 0
file = open("scores.txt", "r")
for line in file:
total = total + int(line)
file.close()
print(total)
Reading a file is a loop over its lines. Because each line is text, numbers must be converted with int() or float() before arithmetic, just like keyboard input. Closing the file frees it for other use and ensures data is saved.
Writing to a text file
file = open("log.txt", "w")
file.write("First line\n")
file.write("Second line\n")
file.close()
The crucial point is that "w" replaces the whole file, so to keep existing content you use "a" to append. Unlike print(), write() does not add a newline, so include "\n" where you want line breaks.
Working with CSV files
file = open("members.csv", "r")
for line in file:
fields = line.strip().split(",")
name = fields[0]
age = fields[1]
print(name + " is " + age)
file.close()
The key technique is line.strip().split(","): strip() removes the trailing newline, and split(",") breaks the line into a list of fields accessed by index (fields[0], fields[1], and so on). To write a CSV line, join the fields with commas and add a newline, for example file.write(name + "," + age + "\n").
Try this
Q1. State what input() returns in Python. [1 mark]
- Cue. A string (so it must be converted with
int()orfloat()for numbers).
Q2. State what line.split(",") does to a CSV line. [1 mark]
- Cue. It splits the line into a list of fields, separated at each comma.
Exam-style practice questions
Practice questions written in the style of Pearson Edexcel exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Edexcel 20225 marksA text file called scores.txt has one whole number per line. Write a Python program that reads the file and prints the total of all the numbers.Show worked answer →
Open the file, loop over each line, convert it to an integer and add to a total.
total = 0
file = open("scores.txt", "r")
for line in file:
total = total + int(line)
file.close()
print(total)
Markers reward opening the file for reading, iterating over the lines, converting each line to an integer with int() (because file data is text), accumulating the total, closing the file, and printing the result. Forgetting the int() conversion is a common error.
Edexcel 20215 marksA CSV file members.csv has lines in the form name,age. Write a Python program that reads the file and prints each member's name followed by their age, for example 'Sam is 14'.Show worked answer →
Read each line, strip the newline, split on the comma into fields, then output them.
file = open("members.csv", "r")
for line in file:
fields = line.strip().split(",")
name = fields[0]
age = fields[1]
print(name + " is " + age)
file.close()
Markers reward opening the file, splitting each line on the comma to separate name and age, accessing the two fields by index, building the output, and closing the file. Using split(",") on a CSV line is the key technique.
Related dot points
- Understand the need for and write programs that implement validation (length check, presence check, range check, pattern check) and authentication (ID and password, lookup).
A focused answer to Edexcel GCSE Computer Science 6.4.3 and 6.4.4, covering the need for validation, the length, presence, range and pattern checks, and authentication by ID and password lookup in Python.
- Write programs that manipulate strings (length, position, substrings, case conversion).
A focused answer to Edexcel GCSE Computer Science 6.3.3, covering string manipulation in Python: finding length, accessing characters by position, extracting substrings, and converting case.
- Write programs that make appropriate use of primitive data types (integer, real, Boolean, char) and one- and two-dimensional structured data types (string, array, record), and that make appropriate use of variables and constants.
A focused answer to Edexcel GCSE Computer Science 6.3.1 and 6.3.2, covering the primitive data types (integer, real, Boolean, char), structured types (string, array, record) in one and two dimensions, and using variables and constants.
- Identify the structural components of programs (constants, variables, initialisation, assignment, sequence, selection, repetition, iteration, data structures, subprograms, parameters, input/output) and write programs that use sequencing, selection, repetition (count-controlled, condition-controlled) and iteration with single entry and exit points.
A focused answer to Edexcel GCSE Computer Science 6.2, covering the structural components of programs and writing Python programs that use sequence, selection, count-controlled and condition-controlled repetition, and iteration.
- Use decomposition and abstraction to solve problems; read, write, analyse and refine programs; convert algorithms (flowcharts, pseudocode) into programs; use techniques (layout, indentation, comments, meaningful identifiers, white space) for readable code; identify, locate and correct logic, syntax and runtime errors; and evaluate a program's fitness for purpose and efficiency.
A focused answer to Edexcel GCSE Computer Science 6.1, covering using decomposition and abstraction in programs, converting algorithms to code, writing readable code, identifying and correcting errors, and evaluating fitness for purpose.
Sources & how we know this
- Pearson Edexcel GCSE (9-1) Computer Science (1CP2) specification — Pearson (2020)
- Pearson Edexcel GCSE Computer Science Programming Language Subset (Python) — Pearson (2020)