← VCE Software Development U3O1

Writing records to a CSV file

Joins a record's fields, which must all be strings, into one comma-separated line. Use str() on numbers first, then write the line plus a newline to the CSV file.

Understanding Writing records to a CSV file in VCE Software Development U3O1

What it does: The header row is just a comma-separated line of column names written before any records, ending with a newline. Mode 'a' means append, so new lines are added to the end. Mode 'w' would overwrite the existing data. The separator comes first: ','.join(record) puts a comma between each field in the list. record.join(',') is wrong because lists do not have a join method. join() only accepts strings. The number 16 must be converted with str() first, otherwise Python raises a TypeError. Every field must be a string. str(16) turns the integer into '16', so join() can handle it. 'a' appends to the end of the file. 'w' overwrites the file, so all earlier records would be lost.

Common mistakes: Mode "r" opens the file for reading only, so f.write() would fail. To save records the file must be opened in write mode: open("pupils.csv", "w"). 15 is an integer, and join() only accepts strings, so line 2 would crash with a TypeError. Convert the number first with str(15).

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
Which line joins the fields of a record list called record into one CSV line?
line = record.join(',')line = ','.join(record)
Matches
line = ','.join(['Ava', 88])
↓
The number 88 is not a string, so join() causes a TypeError. Use str(88) first.
Applying
Meanings
Term
','.join(record)
Meaning
Joins a record's fields, which must all be strings, into one comma-separated line. Use str() on numbers first, then write the line plus a newline to the CSV file.
Practice
header = "name,age,mark"
Testing
Gaps
line = ','.(record)
Check Answer
Spots
1f = open("pupils.csv", "r")
2row = ["Ava", "16", "Red"]

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

The 'Galaxy Phoenix' badge — Orbit Champion, 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. Which line joins the fields of a record list called record into one CSV line?
A. line = ','.join(record) ✓
B. line = record.join(',')
The separator comes first: ','.join(record) puts a comma between each field in the list. record.join(',') is wrong because lists do not have a join method.
2. record = ['Ava', 16, 'Red']. What happens when you run ','.join(record)?
A. 'Ava,16,Red'
B. TypeError, because 16 is not a string ✓
join() only accepts strings. The number 16 must be converted with str() first, otherwise Python raises a TypeError.
3. Which record is safe to pass to ','.join()?
A. ['Ava', str(16), 'Red'] ✓
B. ['Ava', 16, 'Red']
Every field must be a string. str(16) turns the integer into '16', so join() can handle it.
4. You write a line to a CSV file with file.write(line). What is missing so each record sits on its own row?
A. file.write(line + '\n') ✓
B. file.write(line + ',')
A newline character '\n' ends the row. A trailing comma would just add an empty extra field and keep everything on one line.
5. Which approach writes a header row to a new CSV file correctly?
A. file.write(['name', 'age'])
B. file.write(','.join(['name', 'age']) + '\n') ✓
The header is just another record of strings. Join it with commas and add a newline. write() needs a string, not a list.

Match the Pairs

','.join(record) → Joins the string fields of a record into one comma-separated line ready to write to a CSV file
str(record[2]) → Converts a number field to a string so it can be joined into a CSV line
Write a header row first so the CSV file has column names, then the data lines → file.write('name,mark\n')
Append a new record to the end of an existing CSV file without erasing the old lines → open('marks.csv', 'a')
line = ','.join(['Ava', 88]) → The number 88 is not a string, so join() causes a TypeError. Use str(88) first.
file.write(','.join(record)) → No newline is added, so every record ends up on the same line. Add + '\n' after the joined text.

Key Term

','.join(record)
Joins a record's fields, which must all be strings, into one comma-separated line. Use str() on numbers first, then write the line plus a newline to the CSV file.

Fill in the Blanks

1. line = ','.join(record)
Hint: Type the string method that combines a record's fields into one line.
2. line = ','.join([name, str(score)])
Hint: Type the function that converts a number to text so it can be joined.
3. row = [name, str(score)] f.write(','.join(row) + '\n')
Hint: Type the file method that saves the joined line to the CSV file.
4. with open('scores.csv', 'a') as f: f.write(','.join(row) + '\n')
Hint: Type the single-letter file mode that adds a new record to the end without erasing existing data.
5. header = ','.join(['Name', 'Score']) f.write(header + '\n')
Hint: Type the variable that holds the joined header row so it is written first.

Spot the Error

Question 1
A f = open("pupils.csv", "r")
B row = ["Ava", "16", "Red"]
C line = ",".join(row)
D f.write(line + "\n")
E f.close()
Line A — Mode "r" opens the file for reading only, so f.write() would fail. To save records the file must be opened in write mode: open("pupils.csv", "w").
Question 2
A row = ["Liam", 15, "Blue"]
B line = ",".join(row)
C with open("p.csv", "a") as f:
D f.write(line + "\n")
Line A — 15 is an integer, and join() only accepts strings, so line 2 would crash with a TypeError. Convert the number first with str(15).
Question 3
A with open("s.csv", "w") as f:
B f.write("item price\n")
C row = ["Pen", str(1.5)]
D line = ",".join(row)
E f.write(line + "\n")
Line B — The header row must use commas to separate the column names, like the data rows. It should be f.write("item,price\n").
Question 4
A row = ["Zoe", str(88), "A"]
B line = row.join(",")
C f = open("marks.csv", "a")
D f.write(line + "\n")
E f.close()
Line B — join() is a string method, and lists have no join method. The separator string comes first and the record goes inside the brackets: ",".join(row).
Question 5
A f = open("kids.csv", "w")
B r = ["Al", "7"]
C f.write(",".join(r) + "/n")
D f.close()
Line C — The newline character is \n with a backslash. "/n" would write the literal characters /n into the file instead of ending the line. It should be "\n".

Student with a class code? Join your class →