← VCE Software Development U3O1

Reading a CSV file into an array of records

Splits one CSV line into fields and appends them as one record to a list, so each row of the file becomes a record and the whole file becomes a table in memory.

Understanding Reading a CSV file into an array of records in VCE Software Development U3O1

What it does: int() converts the text field at index 2 into a whole number so it can be used in arithmetic and comparisons. csv.reader returns each line as a list of fields, replacing the need to call .split(',') yourself. next(file) reads and discards the first line. records is still empty before the loop, so pop() would cause an error. Without strip(), the last field would end with '\n'. The commas are what split(',') uses to find the fields. int() is a function that takes the string as an argument. Strings have no .int() method, so the second option causes an error. Using = replaces records on every pass, so only the last row would be kept. append() adds each row to the list.

Common mistakes: A CSV file separates fields with commas, so the line must be split on ','. Splitting on a space leaves each row as one big field. It should be split(','). A with statement must end with a colon. Without it Python raises a SyntaxError. It should be: with open('marks.csv') as f:.

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 turns one CSV line into a record of fields and adds it to a list?
records.split(line.strip().append(','))records.append(line.strip().split(','))
Matches
records = [] for line in file: records.append(line)
↓
The whole line is appended as one long string. It is never split into fields, so the list is not a table of records.
Applying
Meanings
Term
records.append(line.strip().split(','))
Meaning
Splits one CSV line into fields and appends them as one record to a list, so each row of the file becomes a record and the whole file becomes a table in memory.
Practice
records = []
Testing
Gaps
records.append(line.strip().(','))
Check Answer
Spots
3for ln in f:
4 p = ln.strip().split(' ')

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

The 'Oryx' badge — Desert Navigator, 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 turns one CSV line into a record of fields and adds it to a list?
A. records.split(line.strip().append(','))
B. records.append(line.strip().split(',')) ✓
strip() removes the newline, split(',') breaks the line into a list of fields, and append() adds that list to records as one record.
2. After reading a CSV file with 5 data rows into records, what does records[0] hold?
A. The fields of the first row, as a list ✓
B. The whole file as one long string
Each row is appended as its own list of fields, so records[0] is one record (the first row) and the whole list is a table in memory.
3. A CSV file has a header row: name,mark. How do you skip it while reading?
A. Call next(file) once before the loop ✓
B. Call records.pop() before the loop
next(file) reads and discards the first line. records is still empty before the loop, so pop() would cause an error.
4. Why does the line.strip() part matter in line.strip().split(',')?
A. It removes every comma from the line
B. It removes the trailing newline so the last field is clean ✓
Without strip(), the last field would end with '\n'. The commas are what split(',') uses to find the fields.
5. Fields read from a CSV file are always strings. Which converts the mark field to a number?
A. int(fields[1]) ✓
B. fields[1].int()
int() is a function that takes the string as an argument. Strings have no .int() method, so the second option causes an error.

Match the Pairs

next(reader) → Skips the header row by reading and discarding the first line before the loop reads the data rows
records.append(line.strip().split(',')) → Removes the newline, splits the line at each comma and adds the fields to the list as one record
Each field is read from a file as a string, so a numeric field must be converted before it is used in calculations → record[2] = int(record[2])
The csv module reader splits every line into a list of fields for you, so you can loop over the rows directly → for row in csv.reader(file):
records = [] for line in file: records.append(line) → The whole line is appended as one long string. It is never split into fields, so the list is not a table of records.
for line in file: records.append(line.strip().split(',')) # header row is included → The header row is not skipped, so the column names are stored as a record and mixed in with the real data.

Key Term

records.append(line.strip().split(','))
Splits one CSV line into fields and appends them as one record to a list, so each row of the file becomes a record and the whole file becomes a table in memory.

Fill in the Blanks

1. for line in file: records.append(line.strip().split(','))
Hint: Type the string method that breaks a line into a list of fields at each comma.
2. file = open('students.csv') next(file) for line in file:
Hint: Type the built-in function that moves past the first (header) line before the loop starts.
3. parts = line.strip().split(',') records.append([parts[0], int(parts[1])])
Hint: Type the function that converts the text field into a whole number before it is stored.
4. import csv reader = csv.reader(file) records = list(reader)
Hint: Type the csv module function that reads each row of the file as a list of fields.
5. records = [] for line in file: records.append(line.strip().split(','))
Hint: Type the string method that removes the newline character from the end of each line.

Spot the Error

Question 1
A rows = []
B f = open('data.csv')
C for ln in f:
D p = ln.strip().split(' ')
E rows.append(p)
Line D — A CSV file separates fields with commas, so the line must be split on ','. Splitting on a space leaves each row as one big field. It should be split(',').
Question 2
A records = []
B with open('marks.csv') as f
C for line in f:
D p = line.split(',')
E records.append(p)
Line B — A with statement must end with a colon. Without it Python raises a SyntaxError. It should be: with open('marks.csv') as f:
Question 3
A f = open('s.csv')
B f.next()
C rows = []
D for ln in f:
E rows.append(ln.split(','))
Line B — File objects have no .next() method in Python 3, so this raises an AttributeError. Use the built-in next(f) to skip the header row.
Question 4
A records = []
B for ln in open('s.csv'):
C p = ln.strip().split(',')
D int(p[1])
E records.append(p)
Line D — int(p[1]) creates a number but the result is thrown away, so the field stays a string. Assign it back: p[1] = int(p[1]).
Question 5
A import csv
B with open('s.csv') as f:
C reader = csv.read(f)
D rows = list(reader)
Line C — The csv module has no read() function. The function that reads rows as lists of fields is csv.reader(f).

Student with a class code? Join your class →