Published lesson · 30 minutes

Read and write simple records

Objective: Parse a small CSV-style record safely and report missing or malformed values without crashing.

Understand the idea

Read and write simple records works best when the learner can explain the reason behind each step, not just copy a result. Input validation checks whether data has the shape a program expects; file-like text should be treated as untrusted and bounded.

A volunteer copies a short synthetic attendance list into a practice file and wants to count records with a valid name. Treat the names and details as invented practice information; the example is for learning a method and does not describe a live service.

A dependable sequence is: Split or parse each row, trim whitespace, check the required columns and skip or explain a malformed row. Write down the important decision before you begin so another person can follow the reasoning.

Do not include real learner names in examples. Handle empty lines and incorrect numbers explicitly, and do not overwrite the original input. Compare the result with the objective, then change one thing at a time if the first attempt does not work.

Add one malformed sample row and describe how the program should report it. Explain one assumption that would need checking before anyone used a similar result outside the classroom.

Keep a short record of the choices you made and the evidence you used to check them. A useful practice result is understandable by another learner, stays within the stated scope and makes its limitations visible before anyone relies on it.

Worked examples

Read and write simple records: practice example

Explore how read and write simple records supports the lesson objective with a small synthetic example.

main.py

records = [{"name": "Room hire", "amount": 45}, {"name": "Printing", "amount": 12}]
total = sum(record.get("amount", 0) for record in records)
print(f"Practice total: R{total}")

Practice tasks

  1. Explore read and write simple records

    Use the worked example for “Read and write simple records”. Change one small detail to suit a different synthetic community situation, then explain what you expected and how you checked whether the change still met the lesson objective.

  2. Build a Python practice example

    Create a small practice result that demonstrates this objective: Parse a small CSV-style record safely and report missing or malformed values without crashing. Use only synthetic information, keep the first version focused, and write down one limitation that a later version should address.

  3. Review and explain the result

    Review your “Read and write simple records” result as if another learner will use it without your explanation. Identify one strength, one exact point that may be unclear, and a revision that would make the result easier to understand or use.

Read and write simple records becomes easier when you split or parse each row, check the result and explain the choice to another learner.

Course overview