#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
# on définie le fichier
data = {'liste': [10, 20, 30, 40],
'phrase': 'demain il fait beau',
'tableau': {'chien': 1,
'chat': 3,
'serpent': 4}}
# écrire dans un fichier
with open('fichier.json', 'w') as fichier:
ecriture = json.dumps(data,
indent=5, sort_keys=True,
separators=(',', ':'), ensure_ascii=False)
fichier.write(ecriture)
# lire un fichier .json
with open('fichier.json') as fichier:
lecture = json.load(fichier)
print(data == lecture) # on vérifie l'écriture
print(lecture['liste'][3]) # sortir de la liste la 3ème colonne (0,1,2,3) donc 40.
print(lecture['phrase']) # sortir la phrase
print(lecture['phrase'][0:6]) # sortir un bout de la phrase
print(lecture['tableau']['chat']) # sortir ligne d'un tableau
exit