Python: How To Change All Instances Of "timestamp" In .json File To A Date-time Object
I have a LocationHistory.json file that has location data stored. The data looks like this: { 'data' : { 'items' : [ { 'kind' : 'latitude#location', 'timestampMs' : '
Solution 1:
To get POSIX timestamps from a json file and convert them into naive datetime objects that represent UTC time:
#!/usr/bin/env pythonimport 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')
Output
2013-07-26T20:34:56.803000Z2013-07-26T20:31:51.762000Z
Notice: milliseconds are preserved.
Post a Comment for "Python: How To Change All Instances Of "timestamp" In .json File To A Date-time Object"