我想創建一個單獨的文件,將多個可以每天更新的有關沿海天氣條件(潮汐,風力等)的python字典組合起來。該數據來自多個API和網站,每一個我用下面的代碼行轉換爲Python字典,併合併成一個單一的詞典:將多個python字典合併爲動態文件
OneDayWeather_data = {'Willydata' : Willydata, 'Bureau of Meteorology' : BoMdata, 'WeatherZone' : WZdata}
我的目標是每天採樣點;並用每天的天氣和網站上的預測更新單個文件。我認爲最好的方法是使用日期爲層次結構創建一個新的頂層。因此,它會像這樣的:
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(S)))當我嘗試重新打開)。希望有一些專業知識的人可以權衡如何解決這個問題。
謝謝!
使用TinyDB怎麼樣? http://tinydb.readthedocs.io/en/latest/ – Grimmy