Aller au contenu principal

Le site sera en maintenance le 31/05/2026

Aller au contenu principal
Formation complète Python de zéro à héros
2 min de lecture
0 commentaires
Gratuit

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])

Commentaires (0)

Laisser un commentaire

Aucun commentaire pour le moment. Soyez le premier à commenter !