【问题标题】:How to save json all update data with python如何使用python保存json所有更新数据
【发布时间】:2023-03-02 21:14:01
【问题描述】:

simple.py

import json, urllib.request
url = "https://datahead.herokuapp.com/api/employeers/"
response = urllib.request.urlopen(url)
data = json.loads(response.read())

var_json = ?
for i in data:
    print('\n')
    for key, value in i.items():
        print(key,':', value)

我的json数据在这里,你可以访问我的api链接。

id : 1
name : Prosenjit Das
log_date : 2019-03-02
log_time : 12:10:12.247257
login : None
logout : None


id : 2
name : Sudipto Rahman
log_date : 2019-03-02
log_time : 12:10:12.247257
login : 11:26:45
logout : 10:49:53

有时我的数据会更新。现在,我想保存每个历史数据,就好像我可以访问这些数据进行计算一样。 在这种情况下必须使用任何数据库或没有我可以存储或保存的数据库?

谢谢。

【问题讨论】:

    标签: json database python-3.x api web.py


    【解决方案1】:
    import json
    
    ## you can store data
    with open('data.json','w') as f:
        json.dump(f,data)
    
    
    ## and can read data from that json file
    with open('data.json','r') as f:
        old_data = json.load(f)
    
    
    ## you can compare both data and overwrite data.json if required
    

    【讨论】:

    • 感谢 Akhilesh。但是在这种情况下,我想计算每个 dict 项 nad 是不可能检索到准确数据的。例如:包含2个id登录和注销时间,2个id注销了多少次以及这个日期的总值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2022-01-24
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多