"""国税庁 法人番号公表サイトの全国CSV（Shift-JIS, 2026-09-30時点）から、現存法人数を数える。

使い方: python3 count_houjin.py 00_zenkoku_all_20260930.csv
現存 = latest列が1、閉鎖日(close_date)が空、処理区分が99(削除)でない行。
"""
import csv
import collections
import json
import sys

rows = active = 0
kinds = collections.Counter()
pref = collections.Counter()
with open(sys.argv[1], encoding="cp932", errors="replace", newline="") as f:
    for r in csv.reader(f):
        rows += 1
        if r[23] != "1" or r[18] or r[2] == "99":
            continue
        active += 1
        kinds[r[8]] += 1
        pref[r[9]] += 1
print(json.dumps({"rows": rows, "active": active, "kinds": kinds, "pref": pref}, ensure_ascii=False, indent=1))
