MCPcopy Create free account
hub / github.com/RASAAS/docmcp-knowledge / walk_table

Function walk_table

scripts/_debug_eurlex_table.py:28–59  ·  view source on GitHub ↗
(tbl, depth=0)

Source from the content-addressed store, hash-verified

26# Better approach: only process direct rows, handle nested tables recursively
27print("\n=== PROPOSED: recursive table walker ===")
28def walk_table(tbl, depth=0):
29 prefix = " " * depth
30 # Get direct rows only
31 tbody = tbl.find("tbody", recursive=False)
32 container = tbody if tbody else tbl
33 rows = [c for c in container.children if isinstance(c, Tag) and c.name == "tr"]
34
35 for row in rows:
36 cells = row.find_all(["td", "th"], recursive=False)
37 if not cells:
38 continue
39
40 # For each cell, separate direct text from nested tables
41 for cell in cells:
42 inner_tables = cell.find_all("table", recursive=False)
43
44 # Get text excluding nested tables
45 direct_text = ""
46 for child in cell.children:
47 if isinstance(child, Tag) and child.name == "table":
48 continue
49 if isinstance(child, Tag):
50 direct_text += child.get_text(" ", strip=False)
51 else:
52 direct_text += str(child)
53 direct_text = re.sub(r"\s+", " ", direct_text).strip()
54
55 if direct_text:
56 print(f"{prefix}TEXT: '{direct_text[:100]}'")
57
58 for it in inner_tables:
59 walk_table(it, depth + 1)
60
61walk_table(table)

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected