====== JSON ====== * [[https://docs.python.org/fr/3/library/json.html]] * [[https://stackabuse.com/reading-and-writing-json-to-a-file-in-python/|Reading and Writing JSON to a File in Python]] | data = **json.load**(f, ...) | Décodage depuis un fichier | | data = **json.loads**(s, ...) | Décodage depuis une chaîne | | **json.dump**(f, data, ...) | Encodage vers un fichier | | **json.dumps**(data, ...) | Encodage vers une chaîne | ===== Encodage ===== import json data = [ 'foo', { 'bar': ('baz', None, 1.0, 2) } ] j = json.dumps(data) Avec indentation : j = json.dumps(data, indent=4) print(j) Vers un fichier : with open('data.txt', 'w') as f: json.dump(data, f, indent=4) ===== Décodage ===== Depuis une chaîne : import json data = json.loads(str) Depuis un fichier : import json with open("/home/marc/fichier.json", 'r') as f: data = json.load(f)