import os, sys, re sys.stdout.reconfigure(encoding='utf-8') fpath = 'resources/knowledge/original/원가계산/건설공사_표준품셈/02_토목부문/제9장_측량.md' with open(fpath, 'r', encoding='utf-8') as f: lines = f.readlines() tables = [] cur_t = [] cur_start = 0 cur_title = "" for idx, l in enumerate(lines): line_str = l.strip() if not line_str.startswith('|'): if line_str and not line_str.startswith('>'): cur_title = line_str if cur_t: tables.append((cur_start, cur_title, cur_t)) cur_t = [] else: if not cur_t: cur_start = idx + 1 cur_t.append((idx + 1, line_str)) if cur_t: tables.append((cur_start, cur_title, cur_t)) print(f"Total tables in Survey: {len(tables)}") # Check for multi-level hierarchical headers (Col 0 and Col 1) hierarchical_tables = [] for t_start, t_title, t_rows in tables: data_rows = [] for lno, r_str in t_rows: cells = [c.strip() for c in r_str.split('|')[1:-1]] if all(re.match(r'^:?-+:?$', c) for c in cells if c): continue data_rows.append((lno, r_str, cells)) if len(data_rows) < 3: continue # Check if Col 0 has blanks or ditto marks with changing Col 1 has_blank_col0 = False col0_vals = [] for lno, r_str, cells in data_rows[1:]: c0 = cells[0] if len(cells) > 0 else "" if c0 == "" or c0 == "〃": has_blank_col0 = True elif c0: col0_vals.append(c0) if has_blank_col0 and len(set(col0_vals)) >= 2: hierarchical_tables.append((t_start, t_title, len(data_rows), set(col0_vals))) print(f"Hierarchical / Grouped tables in Survey: {len(hierarchical_tables)}") for start, title, rcnt, groups in hierarchical_tables[:20]: print(f"L{start}: {title[:40]} | Rows: {rcnt} | Groups: {list(groups)[:4]}")