Spaces:
Running
Running
| import json | |
| import os | |
| def verifier_et_extraire(filepath): | |
| if not os.path.exists(filepath): | |
| print(f"❌ Erreur : Le fichier '{filepath}' est introuvable.") | |
| return | |
| try: | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| data = json.load(f) | |
| # Le fichier peut être une liste ou un objet unique | |
| if isinstance(data, dict): | |
| data = [data] | |
| print(f"✅ Fichier chargé avec succès ! Il contient {len(data)} racine(s).") | |
| # Fonction récursive pour afficher l'arbre dans la console | |
| def afficher_arbre(nodes, niveau=0): | |
| for node in nodes: | |
| name = node.get("name", "Sans nom") | |
| children = node.get("children", []) | |
| # Affichage console avec indentation | |
| print(" " * niveau + f"|- {name}") | |
| if children: | |
| afficher_arbre(children, niveau + 1) | |
| print("\n--- Structure de votre nomenclature ---") | |
| afficher_arbre(data) | |
| print("\n---------------------------------------") | |
| except json.JSONDecodeError: | |
| print("❌ Erreur : Le fichier n'est pas un JSON valide.") | |
| except Exception as e: | |
| print(f"❌ Erreur inattendue : {e}") | |
| # Lancement | |
| verifier_et_extraire("nomenclature.txt") |