import csv import io # Read file with open(r'D:\EnforcementCodeProject\CodeV2\EnforcementCode-2.0\doc\数据权限控制\原始数据.csv', 'r', encoding='utf-8') as f: content = f.read() # Parse paths from CSV, store vir_id and vir_zone for each path endpoint path_info = {} # path -> (vir_id, vir_zone) reader = csv.reader(io.StringIO(content)) for row in reader: if len(row) >= 8: path = row[6].strip() vir_id = row[0].strip() vir_zone = row[4].strip() if path and path.startswith('深圳市'): path_info[path] = (vir_id, vir_zone) # Build tree - store children as ordered dict tree = {} for path in sorted(path_info.keys()): parts = path.split('/') node = tree for part in parts: if part not in node: node[part] = {} node = node[part] # Build a lookup: full_path -> (vir_id, vir_zone) for each node # We need to map each node in the tree to its path to find info node_info = {} # tuple of path parts -> (vir_id, vir_zone) for path, info in path_info.items(): parts = tuple(path.split('/')) node_info[parts] = info # Generate ASCII tree lines = [] def render_tree(node, prefix='', is_root=True, path_parts=()): keys = sorted(node.keys()) for i, key in enumerate(keys): is_last_item = (i == len(keys) - 1) current_path = path_parts + (key,) # Get info suffix info = node_info.get(current_path) suffix = f'({info[0]})({info[1]})' if info else '' if is_root: lines.append(key + suffix) render_tree(node[key], '', False, current_path) else: connector = '└── ' if is_last_item else '├── ' lines.append(prefix + connector + key + suffix) extension = ' ' if is_last_item else '│ ' render_tree(node[key], prefix + extension, False, current_path) render_tree(tree) # Write output output = '# 树型执法单位\n\n```\n' + '\n'.join(lines) + '\n```\n' with open(r'D:\EnforcementCodeProject\CodeV2\EnforcementCode-2.0\doc\数据权限控制\树型执法单位.md', 'w', encoding='utf-8') as f: f.write(output) print(f'Done! Total paths: {len(path_info)}, Total lines: {len(lines)}')