使用Python脚本更新Json文件中的时间戳

使用Python脚本更新Json文件中的时间戳,python,json,timestamp,Python,Json,Timestamp,我希望您能帮助我使用Python脚本更新Json文件times.Json,该脚本将更新以下每个时间戳: 对于Id1:CurrentTS-9天、Id2:CurrentTS-7天、Id3:CurrentTS-5天。。等 我试图使用datetime.date.today(),但我就是无法得到一个完整的脚本 [{ "creationTime": 1543647600000, "id":1 }, { "creationTime": 1543647600000, "

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

对于Id1:CurrentTS-9天、Id2:CurrentTS-7天、Id3:CurrentTS-5天。。等

我试图使用
datetime.date.today()
,但我就是无法得到一个完整的脚本

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

在您的代码中,我假设字段“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'])

这是我从问题中了解到的,如果某件事情不是您想要的,请发表评论,我将尝试查看。

非常感谢您的回答。我需要输出的是同一个Json文件,只有请求的更改,它应该是这样的:[{“creationTime”:currentTime-9days,“id”:1},{“creationTime”:currentTime-7days,“id”:2},{“creationTime”:currentTime-2days,“id”:3}]