

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 6
click for more info
Not enough gems
Cost: 6 gems
1: Data Formats
incomplete
2: Parsing JSON
incomplete
3: Variable-Depth JSON
incomplete
4: Fetching JSON
incomplete
5: CSV Files
incomplete
6: CSV Type Conversion
incomplete
7: Filtering CSV Rows
incomplete
8: Writing CSV Files
incomplete
9: Appending to CSV Files
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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)
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.