python - 使用 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
}]
解决方案
在您的代码中,我假设字段“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'])
这是我从问题中了解到的,如果某些事情不是您想要的方式,请发表评论,我会尽力寻找。
推荐阅读
- f#-data - Json Provider 失败,路径中有非法字符
- java - javax.ws.rs.client.WebTarget POST 并同时获取响应
- c# - SSIS 脚本组件目标 - 将表数据拆分到多个文件
- python-3.x - 在特定日期使用 tweepy 获取有关特定哈希值的 Twitter 数据
- python-3.x - 使用余数帮助查找索引?
- mathjax - 为什么 MathJax 设计为异步工作(仅)/如何同步使用 MathJax?
- svn - 仅在 HEAD 修订版 TortoiseSVN 上允许结帐
- c++ - 访问 boost:shared_ptr 在主范围外崩溃,断言失败:px!= 0。指针的正确用法是什么?
- sql - 关于 SQLite 和级联外键的查询
- android - 我应该为哪些 ABI 编译我的原生代码(Android API >= 21)?