← QCE Digital Solutions Unit 2

Streams of structured data

A sequence of structured records that arrives or is read one at a time, such as sensor readings, file lines or API results, and is processed as it flows.

Understanding Streams of structured data in QCE Digital Solutions Unit 2

What it does: The loop body runs once per record. This shows the one-record-at-a-time idea of a stream. Each API result is a structured record. Looping over them handles them one by one, like any other stream. Sensor readings are a classic stream: each reading arrives in order and can be processed as it comes in. API results can be read record by record. A single variable holds one value, not a sequence of records. Processing as the data flows is the key idea of a stream. Structured records share the same fields, so the program knows where to find each value. Because records flow one at a time, processing can begin straight away.

Common mistakes: A for loop header must end with a colon. It should be: for line in file:. range() needs a number, not a list. To read each sensor reading one at a time, loop directly: for r in readings:.

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 186–228) and word search (Modules 144–228).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which best describes a stream of records?
Records that arrive or are read one at a time and processed as they flowAll records loaded into memory at once before any processing starts
Matches
data = open("readings.csv").read() for line in data:
↓
read() loads the whole file into one string, so the loop gets single characters instead of lines. It no longer treats the file as a stream of records.
Applying
Meanings
Term
stream
Meaning
A sequence of structured records that arrives or is read one at a time, such as sensor readings, file lines or API results, and is processed as it flows.
Practice
readings = ["Mon,24", "Tue,27", "Wed,31"]
Testing
Gaps
reading in sensorstream:
Check Answer
Spots
1file = open("temps.csv")
2for line in file

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

The 'Star Fox' badge — Frozen Master, 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 best describes a stream of records?
A. Records that arrive or are read one at a time and processed as they flow ✓
B. All records loaded into memory at once before any processing starts
A stream is handled one record at a time, so you do not need the whole dataset stored first.
2. A temperature sensor sends a new reading every second. Is this a stream?
A. Yes, each reading arrives one at a time as a structured record ✓
B. No, sensor readings can only be processed after the sensor stops
Sensor readings are a classic stream: each reading arrives in order and can be processed as it comes in.
3. A program reads a CSV file by looping over its lines. Each line is treated as:
A. One record in the stream ✓
B. The entire stream at once
Each line in a file is one record. The loop reads them one at a time, so the file acts as the stream.
4. Which is an example of a stream source?
A. A single integer variable called total
B. Results returned by an API, read one record at a time ✓
API results can be read record by record. A single variable holds one value, not a sequence of records.
5. In a stream, when is each record processed?
A. Only after every record has been collected into one big array
B. As it is read, one record at a time ✓
Processing as the data flows is the key idea of a stream.

Match the Pairs

for line in open("readings.csv"): → Reads a file as a stream, giving the program one line (one record) at a time
for reading in sensor_feed: → Processes sensor readings one record at a time as they arrive
A stream is processed one record at a time, so a loop handles each record in turn → for record in stream: print(record)
API results can be treated as a stream of records, with each result processed as it is read → for result in api_results: process(result)
data = open("readings.csv").read() for line in data: → read() loads the whole file into one string, so the loop gets single characters instead of lines. It no longer treats the file as a stream of records.
for reading in sensor_feed: total = reading print(total) → The loop replaces total with each reading instead of adding to it, so nothing builds up as the stream flows. The code should use total = total + reading, after setting total to 0 before the loop.

Key Term

stream
A sequence of structured records that arrives or is read one at a time, such as sensor readings, file lines or API results, and is processed as it flows.

Fill in the Blanks

1. for reading in sensor_stream: print(reading)
Hint: Type the keyword that repeats the loop body once for each item in the stream.
2. for line in open("log.csv"): print(line)
Hint: Type the function that opens a file so its lines can be read one at a time.
3. for record in api_results: print(record)
Hint: Type the keyword that connects the loop variable to the stream of API results it takes records from.
4. total = 0 for r in readings: total = total + r["temp"]
Hint: Type the arithmetic operator that adds each reading's value to the running total.
5. for line in open("data.csv"): fields = line.split(",") print(fields[0])
Hint: Type the string method that breaks one line of the stream into a list of fields.

Spot the Error

Question 1
A file = open("temps.csv")
B for line in file
C parts = line.split(",")
D print(parts[0])
Line B — A for loop header must end with a colon. It should be: for line in file:
Question 2
A total = 0
B for r in range(readings):
C total = total + r
D print(total)
Line B — range() needs a number, not a list. To read each sensor reading one at a time, loop directly: for r in readings:
Question 3
A for line in open("log.csv"):
B fields = line.split(",")
C temp = float(fields(1))
D print(temp)
Line C — A list item is picked with square brackets, not round ones. It should be: temp = float(fields[1])
Question 4
A with open(data.csv) as f:
B for line in f:
C print(line.strip())
Line A — The file name is text, so it needs quotes. Without them Python looks for variables called data and csv. It should be: with open("data.csv") as f:
Question 5
A results = get_results(url)
B for item in results:
C name = item["name"]
D print(Name)
Line D — Variable names are case sensitive. The variable was created as name, so Name does not exist. It should be: print(name)

Student with a class code? Join your class →