CSV text files group data together on individual lines and separates values in these groups by commas.
CSV files can be read by a range of different spreadsheet software packages as well as Python files.
Each item is seperated by a comma, each record is seperated over a new row in the file.
A CSV file is the same as any other text file where each row is stored as string.
Keywords
CSV
comma-separated values are a plaintext data file where each value is separated by a single comma
tabular
displaying data in a grid of rows and columns
strip()
a Python method which removes any leading, and trailing whitespaces or characters that you specify
split()
a Python method which is used to break a string into a list based on a specified delimiter
A common mistake
Mistake:It is not possible to interrogate a CSV file in Python without importing a specialised library.
In fact:A CSV file behaves in the same way as a txt file in Python. Data is read from the file as strings and methods can be used to remove extra characters like commas, spliting the values either side in to separate items.
Check what you learned
From Oak National Academy's exit quiz. Choose every answer that is right.
A computing lesson by Oak National Academy licensed under the Open Government Licence (OGL) v3.0.Read the licence
Lesson 2 of 3 · GCSE level, ages 14 to 16
Reading text files
By the end:I can open and read data from a text file in a program.
A file is a term used to describe data that is held in storage on a computer.
File-handling is a term used to describe how programs work with the files that are stored in a computer.
Different file-handling modes allow you to perform different operations on the contents of a file.
Saving program data to an external file allows the data to be reused when the program runs again.
Keywords
file
a collection of data stored digitally, identified by a name
file-handling
an operation that is specified within a command to open a file, that determines how a file can be used
strip()
a Python method which removes any leading and trailing whitespaces or characters that you specify
A common mistake
Mistake:A text file can be stored anywhere to be used in a program using just the file name.
In fact:A text file must be stored in the same location as a program to be used to be accessed by file name alone. If it is stored elsewhere, the full path of the file will also need to be included.
Check what you learned
From Oak National Academy's exit quiz. Choose every answer that is right.
A computing lesson by Oak National Academy licensed under the Open Government Licence (OGL) v3.0.Read the licence
Lesson 3 of 3 · GCSE level, ages 14 to 16
Writing to text files
By the end:I can write data to a new file and append data to an existing text file.
There are different modes for opening a file, r (read), w (write) and a (append).
When you open a file in write mode, it creates a new file. If the file already exists then the file will be overwritten.
When you open a file and write to a file in append mode, it will add to the existing contents of the file.
It is good practice to close the file once written to. Some IDEs will not write to a file until it is closed.
Keywords
write
a file handling mode that allows a program to write data to a file, where, if the file does not exist, a new file is created, and if the file already exists, the content is overwritten
append()
a file handling mode that allows a program to write extra data at the end of a file without erasing the existing contents of the file
overwrite
to replace information in (a computer file) with new information
A common mistake
Mistake:You have to create a new write() line for each line you want to add to a file.
In fact:You can write mutiple lines in one write() statement. But if you want the text to appear on separate lines, you must use the new line character.
Check what you learned
From Oak National Academy's exit quiz. Choose every answer that is right.
A computing lesson by Oak National Academy licensed under the Open Government Licence (OGL) v3.0.Read the licence
The wording comes from each publisher. The layout and the links to your syllabus are ours.