From 8f75da47fed63788220fc87482b6f4f305f9949a Mon Sep 17 00:00:00 2001 From: Jens Tirsvad Nielsen Date: Tue, 13 Oct 2026 15:28:09 +0800 Subject: [PATCH] Read the weather rows and temperatures with the csv library Task: MIL-002#2 Task: MIL-002#3 --- src/weather/csv_reader.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/weather/csv_reader.py diff --git a/src/weather/csv_reader.py b/src/weather/csv_reader.py new file mode 100644 index 0000000..052dba9 --- /dev/null +++ b/src/weather/csv_reader.py @@ -0,0 +1,35 @@ +## @file csv_reader.py +# @brief Reads the weather data file with Python's built-in `csv` library. +# +# This is the first half of the first lecture: the rows are read with +# `csv.reader` and the temperatures are converted to whole numbers. The module +# needs no third-party dependency. + +"""Reads the weather data file with the built-in csv library.""" + +import csv +from pathlib import Path + +from weather.constants import DATA_FILE, FILE_ENCODING, TEMP_COLUMN_INDEX + + +def read_rows(path: Path | str = DATA_FILE) -> list[list[str]]: + """! @brief Read every row of a CSV file, the header row included. + + @param path Path of the CSV file; the weather data file by default. + @return The rows, each a list of strings, in file order. + """ + with open(path, newline="", encoding=FILE_ENCODING) as file: + return [row for row in csv.reader(file) if row] + + +def read_temperatures(path: Path | str = DATA_FILE) -> list[int]: + """! @brief Read the temperatures of a weather CSV file as whole numbers. + + The first row is the header and is skipped. + + @param path Path of the CSV file; the weather data file by default. + @return The temperature of each data row, in file order. + """ + data_rows = read_rows(path)[1:] + return [int(row[TEMP_COLUMN_INDEX]) for row in data_rows]