← VCE Software Development U3O1

Reasons for using TXT, CSV and XML data sources

Choose a data source by structure: TXT for simple lines, CSV for tables of records, XML for nested self-describing data shared between systems. All three store data between runs.

Understanding Reasons for using TXT, CSV and XML data sources in VCE Software Development U3O1

What it does: Each line is just text with no fields or tags, so a TXT file is simple, small and quick to write. Writing to a file stores the data on disk, so it is still there after the program ends. TXT, CSV and XML all do this. CSV stores tables of records, with fields separated by commas. It works well for rows that all have the same fields, and spreadsheets can open it. XML uses tags to describe each value and can nest elements inside other elements. This makes it well suited to sharing structured data between systems. Variables are lost when a program ends. TXT, CSV and XML files all provide persistent storage between runs.

Common mistakes: strip is a method and needs brackets to run. It should be print(line.strip()). Without them the program prints a method object instead of the text line read from the TXT file. A for loop header must end with a colon. It should be for row in rows: so each CSV record is processed in turn.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 98–113) and word search (Modules 72–113).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A program needs to save a simple list of player names, one per line. Which data source is the best fit?
TXT fileXML file
Matches
Use a TXT file to store a table of 500 students with 6 fields each, because TXT is always best
↓
TXT has no field structure, so a table of records is hard to read back. CSV is a better choice because commas separate the fields.
Applying
Meanings
Term
TXT, CSV, XML
Meaning
Choose a data source by structure: TXT for simple lines, CSV for tables of records, XML for nested self-describing data shared between systems. All three store data between runs.
Practice
Ava Nguyen
Testing
Gaps
f = open("notes.", "r")
Check Answer
Spots
2 for line in f:
3 print(line.strip)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Parrotfish' badge — Function Builder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A program needs to save a simple list of player names, one per line. Which data source is the best fit?
A. TXT file ✓
B. XML file
A TXT file stores simple lines of text with no extra structure, so it is the simplest and smallest choice for a plain list.
2. A school wants to store a table of students, with one record per row and the same fields in every row. Which data source suits this best?
A. CSV file ✓
B. TXT file
CSV stores tables of records, with fields separated by commas. It works well for rows that all have the same fields, and spreadsheets can open it.
3. Which data source is the best choice for nested, self-describing data that will be shared between two different systems?
A. CSV file
B. XML file ✓
XML uses tags to describe each value and can nest elements inside other elements. This makes it well suited to sharing structured data between systems.
4. Which statement about saving data in a file is correct?
A. The data is lost when the program closes, like a variable.
B. The data is still there the next time the program runs. ✓
Variables are lost when a program ends. TXT, CSV and XML files all provide persistent storage between runs.
5. Which reason makes CSV a good choice for a large table of records?
A. Each record is one short line, so the file stays small and easy to read. ✓
B. Every value needs opening and closing tags, so the file is self-describing.
Tags belong to XML. CSV has no tags, so it is smaller and simpler, but it does not describe its own fields the way XML does.

Match the Pairs

scores.csv Ava,88,74 Liam,65,70 → A CSV file suits a table of records: one record per line, with fields separated by commas, easy to open in a spreadsheet.
<student><name>Ava</name></student> → An XML file suits nested, self-describing data that is shared between different systems, because the tags label every value.
TXT is the simplest choice for plain lines of text, such as a log or notes, with no fields or structure needed. → log.txt Started game Score saved
Data kept in a file persists between runs, so a program can save its data and load it again next time. → with open('scores.csv', 'w') as f: f.write('Ava,88')
Use a TXT file to store a table of 500 students with 6 fields each, because TXT is always best → TXT has no field structure, so a table of records is hard to read back. CSV is a better choice because commas separate the fields.
Use CSV to store nested data, such as a school with classes that each contain students → CSV is flat, with rows and columns only, so it cannot show nested levels. XML is better because its tags can nest inside each other.

Key Term

TXT, CSV, XML
Choose a data source by structure: TXT for simple lines, CSV for tables of records, XML for nested self-describing data shared between systems. All three store data between runs.

Fill in the Blanks

1. f = open("notes.txt", "r") line = f.readline()
Hint: Type the file extension used for a simple plain-text file of lines.
2. row = "Ava,7,Red" fields = row.split(",")
Hint: Type the string method that breaks a CSV record into a list of fields.
3. <student><name>Ava</name></student>
Hint: Type the element name needed to close the outer XML tag.
4. tree = ET.parse("shop.xml") root = tree.getroot()
Hint: Type the file extension for nested, self-describing data shared between systems.
5. f = open("log.txt", "a") f.write("Saved\n")
Hint: Type the mode letter that adds to the end of a file so data persists between runs.

Spot the Error

Question 1
A with open("todo.txt") as f:
B for line in f:
C print(line.strip)
Line C — strip is a method and needs brackets to run. It should be print(line.strip()). Without them the program prints a method object instead of the text line read from the TXT file.
Question 2
A import csv
B f = open("scores.csv")
C rows = csv.reader(f)
D for row in rows
E print(row[0], row[1])
Line D — A for loop header must end with a colon. It should be for row in rows: so each CSV record is processed in turn.
Question 3
A nested = True
B shared = True
C if nested and shared:
D fmt = "CSV"
E print("Use", fmt)
Line D — Nested, self-describing data shared between systems suits XML. CSV only holds flat tables of records, so it should be fmt = "XML".
Question 4
A f = open("log.txt", "r")
B f.write("Started\n")
C f.write("Saved\n")
D f.close()
Line A — Mode "r" only reads, so the writes would fail and nothing would be stored between runs. Use "w" (or "a") to write lines to the TXT file.
Question 5
A header = "name,year,house"
B row = "Ava,7,Red"
C fields = row.split(";")
D print(fields[0])
Line C — CSV values are separated by commas, so the line must be split on ",". Splitting on ";" leaves the whole record as one field.

Student with a class code? Join your class →