Choose exam board for KS4 Computer Science (GCSE)
Choose exam board for KS4 English
Choose exam board for KS4 French
Choose exam board for KS4 Geography
Choose exam board for KS4 German
Choose exam board for KS4 History
Choose tier for KS4 Maths
Choose exam board for KS4 Music
Choose exam board for KS4 Physical education (GCSE)
Choose exam board for KS4 Religious education (GCSE)
Choose exam board for KS4 Spanish

      Lesson details

      Learning outcome

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

      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.

      Teacher tip

      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.

      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 (2026), 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

      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

      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

      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

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