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:.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
records.append(line.strip().split(','))Fill in the Blanks
Spot the Error
Student with a class code? Join your class →