We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Filtering CSV Rows

CSV files often contain way more rows than you need for the question you're answering. If you only want part of the file, filter rows by skipping them with continue as you read them:

with open("flights.csv", "r", newline="") as file:
    reader = csv.DictReader(file)
    for row in reader:
        if row["on_time"] != "true":
            continue
        print(row["flight_number"])

In this example, only on-time flights make it to print(row["flight_number"]). If you need to do something later with the matching rows, append them to a list instead:

delayed_flights = []
with open(filename, "r", newline="") as file:
    reader = csv.DictReader(file)
    for row in reader:
        delay_minutes = float(row["delay_minutes"])
        if delay_minutes <= 30.0:
            continue
        delayed_flights.append(row)

Assignment

SnackStack has a CSV of device readings, but only some devices are relevant for the current analysis. You need to filter rows based on device characteristics.

Complete the filter_high_temperature_devices function. It accepts a list of device reading dictionaries and a temperature threshold.