Created
December 9, 2025 20:13
-
-
Save richardadalton/88348729d45d160168ecd5d55a309ce7 to your computer and use it in GitHub Desktop.
Use csv DictReader and DictWriter to read, and write a csv files, dropping rows with missing data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| IN_PATH = "values.csv" | |
| OUT_PATH = "values_cleaned.csv" | |
| def is_number(s: str) -> bool: | |
| try: | |
| float(s) | |
| return True | |
| except ValueError: | |
| return False | |
| kept = 0 | |
| dropped = 0 | |
| with open(IN_PATH, newline="", encoding="utf-8") as src, open(OUT_PATH, "w", newline="", encoding="utf-8") as dst: | |
| reader = csv.DictReader(src) | |
| fieldnames = reader.fieldnames or ["date", "min", "max"] | |
| writer = csv.DictWriter(dst, fieldnames=fieldnames) | |
| writer.writeheader() | |
| for row in reader: | |
| min_val = (row.get("min") or "").strip() | |
| max_val = (row.get("max") or "").strip() | |
| if is_number(min_val) and is_number(max_val): | |
| # preserve trimmed strings for date/min/max | |
| out_row = {k: (row.get(k) or "").strip() for k in fieldnames} | |
| writer.writerow(out_row) | |
| kept += 1 | |
| else: | |
| dropped += 1 | |
| print(f"Wrote {kept} rows to `{OUT_PATH}`; dropped {dropped} invalid rows.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment