Created
July 21, 2026 12:32
-
-
Save rmoriz/8be9c353701bc382b4dc67a47789f90d to your computer and use it in GitHub Desktop.
wegli-filter.py
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 argparse | |
| import csv | |
| import sys | |
| PLZ_DEFAULT_MIN = 80331 | |
| PLZ_DEFAULT_MAX = 81929 | |
| parser = argparse.ArgumentParser(description="Filter wegli CSV by PLZ range (default: München)") | |
| parser.add_argument("input", help="Input CSV file") | |
| parser.add_argument("-o", "--output", default=None, help="Output CSV file (default: notices-muenchen.csv)") | |
| parser.add_argument("--plz-min", type=int, default=PLZ_DEFAULT_MIN, help=f"Min PLZ (default: {PLZ_DEFAULT_MIN})") | |
| parser.add_argument("--plz-max", type=int, default=PLZ_DEFAULT_MAX, help=f"Max PLZ (default: {PLZ_DEFAULT_MAX})") | |
| parser.add_argument("--year", type=int, default=None, help="Filter by year of start_date (e.g. 2024)") | |
| args = parser.parse_args() | |
| OUTPUT = args.output or "notices-muenchen.csv" | |
| with open(args.input, newline="", encoding="utf-8") as infile, open(OUTPUT, "w", newline="", encoding="utf-8") as outfile: | |
| reader = csv.reader(infile) | |
| writer = csv.writer(outfile) | |
| header = next(reader) | |
| writer.writerow(header) | |
| plz_idx = None | |
| start_idx = None | |
| for i, col in enumerate(header): | |
| c = col.strip().lower() | |
| if c in ("plz", "zip", "postal_code", "postleitzahl"): | |
| plz_idx = i | |
| if c in ("start_date", "start"): | |
| start_idx = i | |
| if plz_idx is None: | |
| print("Fehler: Keine PLZ-Spalte im Header gefunden.") | |
| sys.exit(1) | |
| count = 0 | |
| for row in reader: | |
| try: | |
| plz = int(row[plz_idx]) | |
| if args.plz_min <= plz <= args.plz_max: | |
| if args.year is not None and start_idx is not None: | |
| if not row[start_idx].startswith(str(args.year)): | |
| continue | |
| writer.writerow(row) | |
| count += 1 | |
| except (ValueError, IndexError): | |
| pass | |
| label = f"PLZ {args.plz_min}–{args.plz_max}" | |
| if args.year: | |
| label += f", Jahr {args.year}" | |
| print(f"{count} Einträge für {label} nach {OUTPUT} geschrieben.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment