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 44 45
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
fpath = "test.txt"
# exemple de données
data = {
'current_observation':
{
'visibility_mi': '6.2',
'local_tz_short': 'CEST',
'visibility_km': '10.0',
'forecast_url': 'http://www.wunderground.com/global/stations/07015.html',
'precip_today_in': '-999.00',
'wind_string': 'Calm',
'icon_url': 'http://icons.wxug.com/i/c/k/partlycloudy.gif',
'observation_epoch': ' 1408003508',
' temperature_string': '62.6 F (17.0 C)',
'feelslike_string': '62.6 F (17.0 C)',
'precip_1hr_in': '-999.00',
'windchill_string': 'NA',
'heat_index_c': 'NA'
}
}
print("serializing data...")
print(data)
with open(fpath, 'w') as fichier:
# on sérialise les données
json.dump(data, fichier)
# end with
print("\nloading data from file '{}'".format(fpath))
with open(fpath) as fichier:
# on désérialise les données
data2 = json.load(fichier)
# end with
print(data2)
print("\nsame data between output and input?", bool(data == data2)) |
Partager