DoRevision

The CSV File Handler

A CSV file stores a whole table of data as plain text. Learn how a program reads it line by line, splits each record at the commas, and writes new data back to the file.

⏱️ 16 min 🎯 14 activities
Best used for
Homework Intervention Mock preparation

Get the method right under pressure

Free interactive practice on the steps that lose marks under exam pressure.

Start revising free

What you'll cover

Data in a line

How do you store a whole table of data as plain text? You use a CSV file, short for Comma-Separated Values. Each line is one record, and commas separate the pieces of data.

Record, field, delimiter

A few terms describe a CSV file:

Read or write?

A program can open a file in different modes.

The delimiter

In a CSV file, what character separates the fields?

  • A comma
  • A full stop
  • A single space
  • A tab only

Read a real CSV line

  • CSV
  • Record
  • Field
  • Delimiter
  • A file you could open in a text editor and read as it is
  • The single line Ada,Lovelace,1815
  • Just the 1815 part of that line
  • The character sitting between Lovelace and 1815

Reading a file

To read a CSV, a program opens the file, reads a line, and splits that line at the commas to get the separate fields. It repeats for each line, then closes the file. Getting this open, read, split, close order right matters in the exam.

Reading a CSV

Put the steps of reading a record from a CSV file in the right order.

  • Open the file in read mode
  • Read the next line
  • Split the line at the commas
  • Use the fields in the program
  • Close the file

Trace the split

A line holding Sam and 17 is split at the comma. Position 0 is the first field. Fill in each value, and remember the age is still text until it is converted.

Complete the idea

A _____ file stores data as plain text. Each _____ is one line, and a _____ separates the fields. A program that reads a whole line and never _____ it at the commas ends up holding one long string instead of the separate values. To add a new record without deleting the old ones, it opens the file in _____ mode.

CSV record comma splits append image field joins read deletes

About CSV files

Which THREE are true about a CSV file?

  • It is a plain text file
  • Each line is one record
  • Commas separate the fields
  • It is a type of image
  • Fields are separated by full stops

File operations

  • Open
  • Read a line
  • Write
  • Close
  • Get the file ready to use
  • Fetch the next record as text
  • Save a new line of data
  • Finish and free the file

Which operation?

Choose the right file operation for each task.

  • You want to look at the scores already saved, without changing them. Which mode?
  • You want to add a new score to the end without losing the old ones. Which mode?
  • You have read a line and now need the separate fields. What do you do?

True or not?

Tap the TWO statements that are true about CSV files.

  • A CSV file separates its fields with commas.
  • Append mode adds data without deleting what is there.
  • Write mode always keeps the old contents of the file.
  • A CSV file can only ever hold a single record.
  • You must split a line before you can read it.

Explain reading a CSV

Explain how a program reads data from a CSV file.

  • Explain what a CSV file is
  • Explain the order of steps to read it
  • Explain what splitting a line does
  • Explain why the file must be closed