Spaces:
Running
Running
File size: 1,313 Bytes
d655094 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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") |