Python:如何将 .JSON 文件中 "timestamp" 的所有实例更改为日期时间对象
Python: How to change all instances of "timestamp" in .JSON file to a date-time object
我有一个 LocationHistory.json 文件,其中存储了位置数据。数据如下所示:
{
"data" : {
"items" : [ {
"kind" : "latitude#location",
"timestampMs" : "1374870896803",
"latitude" : 34.9482949,
"longitude" : -85.3245474,
"accuracy" : 2149
}, {
"kind" : "latitude#location",
"timestampMs" : "1374870711762",
"latitude" : 34.9857898,
"longitude" : -85.3526902,
"accuracy" : 2016"
}]
}
}
文件中有将近一千个这样的实例,我正在尝试简化这个想法。
然后我通过下面的代码读入数据:
json_file = open('LocationHistory.json')
json_string = json_file.read()
json_data = json.loads(json_string)
locations = json_data["data"]["items"]
现在我想将所有出现的 "timestampMs" 更改为日期时间对象。我通过在 Whosebug 上回答的问题发现以下代码可以帮助我做到这一点:
datetime.datetime.fromtimestamp(
int("timestampMs")
).strftime('%Y-%m-%d %H:%M:%S')
也是这样:
dateObject = datetime.fromtimestap(timestampMs / 1000)
otherFormat = dateObject.strftime("%Y-%m-%dT%H:%M:%SZ")
我的问题是我不熟悉 JSON 并且我不知道如何循环或迭代 LocationHistory.json 文件中出现的所有 "timestampMs" 并更改所有 "timestampsMs" 从 1374870896803 到 2014-09-03.....
我试过了:
for location in locations:
print(datetime.datetime.fromtimestamp(
int("timestampMs")
).strftime("%Y-%m-%dT%H:%M:%SZ")
)
(当我尝试 运行 时出现无效语法错误)
谢谢
要从位置字典中提取数据,您可以使用 get 方法,然后将整数除以 1000 以获得不带毫秒的时间戳:
for location in locations:
print(datetime.datetime.fromtimestamp(
int(location.get("timestampMs"))/1000
).strftime("%Y-%m-%dT%H:%M:%SZ"))
要从 json 文件中获取 POSIX 时间戳并将它们转换为表示 UTC 时间的原始日期时间对象:
#!/usr/bin/env python
import io
import json
from datetime import datetime, timedelta
with io.open('LocationHistory.json', encoding='utf-8') as file:
data = json.load(file)
for item in data['data']['items']:
timestamp_millis = int(item['timestampMs'])
utc_time = datetime(1970, 1, 1) + timedelta(milliseconds=timestamp_millis)
print(utc_time.isoformat() + 'Z')
输出
2013-07-26T20:34:56.803000Z
2013-07-26T20:31:51.762000Z
注意:保留毫秒数。
我有一个 LocationHistory.json 文件,其中存储了位置数据。数据如下所示:
{
"data" : {
"items" : [ {
"kind" : "latitude#location",
"timestampMs" : "1374870896803",
"latitude" : 34.9482949,
"longitude" : -85.3245474,
"accuracy" : 2149
}, {
"kind" : "latitude#location",
"timestampMs" : "1374870711762",
"latitude" : 34.9857898,
"longitude" : -85.3526902,
"accuracy" : 2016"
}]
}
}
文件中有将近一千个这样的实例,我正在尝试简化这个想法。 然后我通过下面的代码读入数据:
json_file = open('LocationHistory.json')
json_string = json_file.read()
json_data = json.loads(json_string)
locations = json_data["data"]["items"]
现在我想将所有出现的 "timestampMs" 更改为日期时间对象。我通过在 Whosebug 上回答的问题发现以下代码可以帮助我做到这一点:
datetime.datetime.fromtimestamp(
int("timestampMs")
).strftime('%Y-%m-%d %H:%M:%S')
也是这样:
dateObject = datetime.fromtimestap(timestampMs / 1000)
otherFormat = dateObject.strftime("%Y-%m-%dT%H:%M:%SZ")
我的问题是我不熟悉 JSON 并且我不知道如何循环或迭代 LocationHistory.json 文件中出现的所有 "timestampMs" 并更改所有 "timestampsMs" 从 1374870896803 到 2014-09-03.....
我试过了:
for location in locations:
print(datetime.datetime.fromtimestamp(
int("timestampMs")
).strftime("%Y-%m-%dT%H:%M:%SZ")
)
(当我尝试 运行 时出现无效语法错误)
谢谢
要从位置字典中提取数据,您可以使用 get 方法,然后将整数除以 1000 以获得不带毫秒的时间戳:
for location in locations:
print(datetime.datetime.fromtimestamp(
int(location.get("timestampMs"))/1000
).strftime("%Y-%m-%dT%H:%M:%SZ"))
要从 json 文件中获取 POSIX 时间戳并将它们转换为表示 UTC 时间的原始日期时间对象:
#!/usr/bin/env python
import io
import json
from datetime import datetime, timedelta
with io.open('LocationHistory.json', encoding='utf-8') as file:
data = json.load(file)
for item in data['data']['items']:
timestamp_millis = int(item['timestampMs'])
utc_time = datetime(1970, 1, 1) + timedelta(milliseconds=timestamp_millis)
print(utc_time.isoformat() + 'Z')
输出
2013-07-26T20:34:56.803000Z
2013-07-26T20:31:51.762000Z
注意:保留毫秒数。