Skip to content

Instantly share code, notes, and snippets.

@richardadalton
Created December 9, 2025 20:13
Show Gist options
  • Select an option

  • Save richardadalton/88348729d45d160168ecd5d55a309ce7 to your computer and use it in GitHub Desktop.

Select an option

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.
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