New
New
Year 11
AQA

Writing to CSV files

I can write data to a new file and append data to an existing text file.

New
New
Year 11
AQA

Writing to CSV files

I can write data to a new file and append data to an existing text file.

These resources will be removed by end of Summer Term 2025.

Switch to our new teaching resources now - designed by teachers and leading subject experts, and tested in classrooms.

These resources were created for remote use during the pandemic and are not designed for classroom teaching.

Lesson details

Key learning points

  1. Writing to a CSV happens line-by-line in the same way as other text files.
  2. When writing to a CSV file you need to convert the data to a single string with commas used as separators.
  3. The join() method takes a list and returns a string.

Keywords

  • List - a dynamic data structure that can contain items of different data types

  • String - a sequence of characters, e.g. letters, numbers, spaces, and other symbols.

  • Join() - the join method takes a list and joins each value in that list into one string.

Common misconception

You can write integers directly to a CSV file using file.write(number).

All data must be converted to strings before writing.


To help you plan your year 11 computer science lesson on: Writing to CSV files, download all teaching resources for free and adapt to suit your pupils' needs...

This lesson has a large section of slides that walk through a section of code. If you prefer, you could get pupils to run this code indvidually and record the values of the list in a trace table or on small whiteboards.
Teacher tip

Equipment

All pupils require access to devices that can edit and run Python programs. Starter code files are available to copy or use directly via the Raspberry Pi Code Editor.

Licence

This content is © Oak National Academy Limited (2025), licensed on Open Government Licence version 3.0 except where otherwise stated. See Oak's terms & conditions (Collection 2).

Lesson video

Loading...

6 Questions

Q1.
What does CSV stand for?
computer system variables
column-separated values
Correct answer: comma-separated values
central spreadsheet viewer
Q2.
Each line in a CSV file is called a .
Correct answer: record
row
column
value
Q3.
What method is used in Python to remove the character from a string?
.remove()
Correct answer: `.strip()`
.cut()
.clean()
Q4.
The `split()` method turns a string into a .
Correct Answer: list
Q5.
What does this code do?
12
line = "Aisha,0" line = line.split(",")
Code colour

When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:


  • • Blue - numbers and boolean values
  • • Green - strings
  • • Purple - keywords
removes commas
Correct answer: breaks the line into a list
joins two strings
Q6.
Using `read()` will read the of a file into one string.
first line
last line
Correct answer: entire contents
specified line

6 Questions

Q1.
Each line in a CSV file represents a new or row.
Correct Answer: record
Q2.
Which error will this code produce?
123
file = open("data.csv", "w") file.write([1, 2, 3]) file.close()
Code colour

When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:


  • • Blue - numbers and boolean values
  • • Green - strings
  • • Purple - keywords
No error
ValueError
Correct answer: TypeError
FileNotFoundError
Q3.
To convert a list of integers to strings, you can use the () function inside a loop.
Correct answer: str
int
print
len
Q4.
What does the
.join()
method do?
combines two files
adds all numbers in a list
joins file paths
Correct answer: converts a list into a single string
Q5.
Match the function with its description.
Correct Answer:`str()`,convert a value to a string
str()
- 

convert a value to a string

Correct Answer:`join()`,combine list items into a string
join()
- 

combine list items into a string

Correct Answer:`write()`,save data to a file
write()
- 

save data to a file

Q6.
To add a new row in a CSV file, you include the special character at the end of the string.
Correct Answer: , n, \ n