Le texte brut (.txt), c'est limité.
Pour échanger des données structurées, on utilise JSON ou CSV.
JSON (JavaScript Object Notation)
C'est le format roi du web. Ça ressemble exactement à un dictionnaire Python.
import json
data = {
"nom": "Bond",
"permis": ["Voiture", "Moto"]
}
# Python vers Fichier JSON (dump)
with open("agent.json", "w") as f:
json.dump(data, f, indent=4)
# Fichier JSON vers Python (load)
with open("agent.json", "r") as f:
data_recup = json.load(f)
print(data_recup["nom"])
CSV (Comma Separated Values)
Le format d'Excel.
import csv
# Écrire
with open("tableau.csv", "w", newline='') as f:
writer = csv.writer(f)
writer.writerow(["Nom", "Age"])
writer.writerow(["Alice", 30])
writer.writerow(["Bob", 25])