82 lines
3.0 KiB
Python
82 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Extract the ISO 3166-1 alpha-3 country-code table from the Wikipedia page.
|
|
|
|
Source (real web_extract result, saved from en.wikipedia.org):
|
|
/root/.hermes/profiles/ledger/cache/web/en.wikipedia.org-8325609c44.md
|
|
|
|
The page's "Officially assigned code elements" section lists every current
|
|
ISO 3166-1 alpha-3 code as a bullet:
|
|
|
|
- ABW [Aruba](https://en.wikipedia.org/wiki/Aruba "Aruba")
|
|
- AFG [Afghanistan](...)
|
|
|
|
Output (written to the artifact dir):
|
|
iso3_alpha3.csv - alpha_3, country_name
|
|
iso3_alpha3.json - same records as a JSON array
|
|
iso3_alpha3.md - rendered Markdown table
|
|
|
|
Only the 249 officially assigned codes are emitted; the user-assigned,
|
|
reserved, deleted and example sections are deliberately excluded.
|
|
"""
|
|
|
|
import csv
|
|
import json
|
|
import os
|
|
import re
|
|
from datetime import datetime
|
|
|
|
SRC = "/root/.hermes/profiles/ledger/cache/web/en.wikipedia.org-8325609c44.md"
|
|
OUT = "/root/.hermes/wizard-outputs/ledger"
|
|
|
|
os.makedirs(OUT, exist_ok=True)
|
|
|
|
# Bullet list item: "- XXX [Country Name]" (country name is the first bracketed
|
|
# title after the code; the URL payload and any trailing material is discarded).
|
|
BULLET_RE = re.compile(r"^- ([A-Z]{3}) \[([^\]]*)\].*$", re.M)
|
|
|
|
in_official_section = False
|
|
records = []
|
|
for line in open(SRC, encoding="utf-8").read().splitlines():
|
|
stripped = line.strip()
|
|
if stripped.startswith("### Officially assigned code elements"):
|
|
in_official_section = True
|
|
continue
|
|
if stripped.startswith("### User-assigned code elements"):
|
|
break
|
|
if in_official_section and BULLET_RE.match(line):
|
|
code, raw = BULLET_RE.match(line).groups()
|
|
country = re.sub(r"\s+", " ", raw).strip()
|
|
records.append({"alpha_3": code, "country_name": country})
|
|
|
|
# Sort alphabetically by country name (deterministic output order).
|
|
records.sort(key=lambda r: r["country_name"])
|
|
|
|
csv_path = os.path.join(OUT, "iso3_alpha3.csv")
|
|
with open(csv_path, "w", encoding="utf-8", newline="") as fh:
|
|
writer = csv.DictWriter(fh, fieldnames=["alpha_3", "country_name"])
|
|
writer.writeheader()
|
|
writer.writerows(records)
|
|
|
|
json_path = os.path.join(OUT, "iso3_alpha3.json")
|
|
with open(json_path, "w", encoding="utf-8") as fh:
|
|
json.dump(records, fh, ensure_ascii=False, indent=2)
|
|
|
|
# Markdown table.
|
|
lines = ["# ISO 3166-1 alpha-3 country-code table", "", "Extracted from the Wikipedia page "
|
|
"\"ISO 3166-1 alpha-3\" (officially assigned code elements).", "",
|
|
"| alpha_3 | country_name |", "|---|---|"]
|
|
for r in records:
|
|
lines.append(f"| {r['alpha_3']} | {r['country_name']} |")
|
|
md_path = os.path.join(OUT, "iso3_alpha3.md")
|
|
with open(md_path, "w", encoding="utf-8") as fh:
|
|
fh.write("\n".join(lines) + "\n")
|
|
|
|
print("rows_emitted:", len(records))
|
|
print("columns:", list(records[0].keys()))
|
|
codes = [r["alpha_3"] for r in records]
|
|
print("dup_codes:", len(codes) - len(set(codes)))
|
|
print("years/order_note:", "n/a", "| first:", records[0]["alpha_3"], "| last:", records[-1]["alpha_3"])
|
|
print("csv:", csv_path)
|
|
print("json:", json_path)
|
|
print("md:", md_path)
|