New
New
Lesson 6 of 9
  • Year 11
  • OCR

Writing to CSV files

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

Lesson 6 of 9
New
New
  • Year 11
  • OCR

Writing to CSV files

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

These resources were made for remote use during the pandemic, not classroom teaching.

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

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

Pupils will need access to a device that can edit and run Python programs. Examples in this lesson use the RPF Code Editor https://oak.link/python-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...

Prior knowledge starter quiz

Download quiz pdf

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

Assessment exit quiz

Download quiz pdf

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

convert a value to a string

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

combine list items into a string

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

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