Files
Aislo/resources/tester/scratch/test_extract_tables.py
T

53 lines
1.8 KiB
Python

import re
import sys
import json
from pathlib import Path
sys.stdout.reconfigure(encoding='utf-8')
md_path = Path("resources/knowledge/original/행정규칙/임도 품셈 적용기준 (현 산림사업 표준품셈)/첨부/(산림청고시 제2025-82호) 산림사업 표준품셈.md")
with open(md_path, "r", encoding="utf-8") as f:
lines = f.readlines()
print(f"Total lines in MD: {len(lines)}")
# Parse markdown tables
# A markdown table is a contiguous block of lines starting with '|' and containing '|---|' separator.
tables = []
current_table = []
start_line = -1
for idx, line in enumerate(lines):
line_str = line.strip()
if line_str.startswith('|') and line_str.endswith('|'):
if not current_table:
start_line = idx + 1
current_table.append((idx + 1, line))
else:
if current_table:
# Check if it has a separator row (|---|...)
has_sep = any(re.match(r'^\|(?:\s*:?-+:?\s*\|)+$', l.strip()) for _, l in current_table)
if has_sep:
tables.append({
'start_line': start_line,
'end_line': current_table[-1][0],
'lines': [l for _, l in current_table]
})
current_table = []
if current_table:
has_sep = any(re.match(r'^\|(?:\s*:?-+:?\s*\|)+$', l.strip()) for _, l in current_table)
if has_sep:
tables.append({
'start_line': start_line,
'end_line': current_table[-1][0],
'lines': [l for _, l in current_table]
})
print(f"Extracted {len(tables)} markdown tables from MD.")
# Check line numbers of first 10 tables
for i, t in enumerate(tables[:10]):
print(f"Table {i+1}: lines {t['start_line']}~{t['end_line']} ({len(t['lines'])} rows)")
print(f" Header: {t['lines'][0].strip()[:80]}")