首页 > 解决方案 > 使用 Python 脚本更新 Json 文件中的时间戳

问题描述

我希望您能帮助我使用 Python 脚本更新 Json 文件times.json,该脚本将更新以下每个时间戳:

对于 Id1:CurrentTS-9days,Id2:CurrentTS-7days,Id3:CurrentTS-5days.. 等

我尝试使用datetime.date.today(),但我无法获得一个完整的脚本。

 [{
    "creationTime": 1543647600000,
    "id":1
    },
{
    "creationTime": 1543647600000,
    "id":2
    },
{
    "creationTime": 1543647600000,
    "id":3
    }]

标签: pythonjsontimestamp

解决方案


在您的代码中,我假设字段“creationTime”是转换为秒的日期,所以我的实现基于此。以下是根据要求更新时间戳的快速代码:

from datetime import datetime, timedelta

data = [{"creationTime": 1543647600000,"id":1},
{"creationTime": 1543647600000,"id":2},
{"creationTime": 1543647600000,"id":3}]

day_start = 9
for tuple in data:
    print('Previous: ' , tuple['creationTime'])
    tuple['creationTime'] -=  int(timedelta(days = day_start).total_seconds())
    day_start -= 2
    print('After: ', tuple['creationTime'])

这是我从问题中了解到的,如果某些事情不是您想要的方式,请发表评论,我会尽力寻找。


推荐阅读