SPB Git forge

spb/wp22_uqo

Public
6commits 1branches 0releases
142.4 MBsize
maindefault branch
3 h agolast push
TeX 55.1% Python 44.2%
2.5 KB · 35 lines python
Raw Blame History
1import openpyxl, csv, sys, collections2downloaded_at, outpath, zipdir = sys.argv[1].strip(), sys.argv[2], sys.argv[3]  # zipdir = folder where MLS_HPI_Oct_2026.zip was unzipped3URL = "https://www.crea.ca/files/mls-hpi-data/MLS_HPI_Oct_2026.zip"4geos = {"AGGREGATE": "Canada (CREA aggregate of participating boards)", "QUEBEC": "Quebec province (CREA aggregate of participating boards)",5        "MONTREAL_CMA": "Montréal CMA", "QUEBEC_CMA": "Québec CMA", "CENTRE_DU_QUEBEC": "Centre-du-Québec", "ESTRIE": "Estrie", "MAURICIE": "Mauricie"}6out = []; cov = collections.defaultdict(lambda: [None, None, 0]); allnames = None7for fname, tag in [("Not Seasonally Adjusted (M).xlsx", "NSA"), ("Seasonally Adjusted (M).xlsx", "SA")]:8    wb = openpyxl.load_workbook(f"{zipdir}/{fname}", read_only=True)9    allnames = wb.sheetnames10    for sh, geo in geos.items():11        ws = wb[sh]; it = ws.iter_rows(values_only=True); hdr = next(it)12        for row in it:13            if row[0] is None: continue14            p = row[0].strftime("%Y-%m")15            if p < "2021-01" or p > "2026-10": continue16            for col, val in zip(hdr[1:], row[1:]):17                if val is None: continue18                is_idx = "HPI" in col19                ptype = col.replace("_HPI_SA","").replace("_Benchmark_SA","").replace("_HPI","").replace("_Benchmark","").replace("_"," ")20                sname = f"MLS HPI {tag} - {col}"21                out.append({"source": "CREA MLS Home Price Index (crea.ca, full-history Excel in MLS_HPI_Oct_2026.zip)",22                            "series_name": sname, "geography": geo,23                            "property_scope": f"{ptype} benchmark (resale, MLS listings, hedonic benchmark; {'seasonally adjusted' if tag=='SA' else 'not seasonally adjusted'})",24                            "frequency": "monthly", "period": p, "value": val,25                            "base_period": "2005-01=100" if is_idx else "CAD (benchmark price)",26                            "download_url": URL, "downloaded_at": downloaded_at})27                k = (geo, sname); c = cov[k]; c[0] = c[0] or p; c[1] = p; c[2] += 128cols = ["source","series_name","geography","property_scope","frequency","period","value","base_period","download_url","downloaded_at"]29with open(outpath, "w", newline="") as f:30    w = csv.DictWriter(f, fieldnames=cols); w.writeheader(); w.writerows(out)31print("all sheets:", allnames)32for k, v in sorted(cov.items()):33    if "Composite" in k[1]: print("coverage", k, v)34print("rows written", len(out))35