日々更新可能な気象条件(潮汐、風など)に関する複数のpython辞書を組み合わせた単一のファイルを作成したい。データは、私はPythonの辞書に変換し、それぞれが、複数のAPIやWebサイトから来て、そして次のコード行を使用して、単一の辞書にマージ:複数のpython辞書を動的ファイルにマージする
OneDayWeather_data = {'Willydata' : Willydata, 'Bureau of Meteorology' : BoMdata, 'WeatherZone' : WZdata}
私の目標は、毎日サイトをサンプリングすることです。毎日の天気予報とサイト全体の予測で1つのファイルを更新します。私はこれを行う最善の方法は、日付を使用して階層に新しいトップレベルを作成することだと思っています。だから、のようなもののようになります:曜日ごと
Weather_data['18/07/2017']['Willy']['Winds']
Weather_data['18/07/2017']['BoMdata']['Winds']
、私は新しい一日のデータのための新しいトップレベルのエントリを追加することになり、すなわち
AllWeatherData['19/07/2017']['Willy']['Winds']
私はのさまざまな方法を使って、これを試してみましたスタックオーバーフローから提案されるメソッド(全開示:私はPythonをかなり慣れています)。例えば、
# write the initial file
with open('test.json', 'w') as f:
json.dump(OneDayWeather_data, f)
# open the initial file and attempt to append
with open('test.json','r+') as f:
dic = dict(json.load(f))
dic.update(OneDayWeather_data)
json.dump(dic, f)
# reopen the appended file
with open('test.json', 'r') as f2:
json_object = json.load(f2)
...しかし、私はエラーを取得しておく(この場合は:私は再度開くしようとするとValueError(ERRMSG( "エクストラデータ"、S、終わり、LEN(複数可))))。いくつかの専門知識がこの問題にどのように接近するかについて重視することができます。
ありがとうございます!あなたが実際にこの段階では、既存の辞書
# write the initial file
import json
OneDayWeather_data = {'a':'b'}
with open('test.json', 'w') as f:
json.dump(OneDayWeather_data, f)
OneDayWeather_data = {'c':'d'}
# open the initial file and attempt to append
with open('test.json','r+') as f:
dic = dict(json.load(f))
dic.update(OneDayWeather_data)
json.dump(dic, f)
# reopen the appended file
with open('test.json', 'r') as f2:
json_object = json.load(f2)
に更新辞書を追加している
TinyDBの使用はどうですか? http://tinydb.readthedocs.io/en/latest/ – Grimmy