I am new to python(Using version 2.7). I am trying to accept mobile sensor data in Json format via python script. The script is storing data in a text file. The following is a part of my Json data.
{"sensor":"Accelerometer","time":1540532851987,"dataZ":"8.2821044921875"}
{"sensor":"Accelerometer","time":1540532852088,"dataZ":"8.162399291992188"}
{"sensor":"Accelerometer","time":1540532852191,"dataZ":"9.026702880859375"}
I can not understand the meaning of the value "time": . How can I convert it to normal time? I have tried the following
import json
import datetime
with open("acc.json") as data_file:
my_dict = [json.loads(line) for line in data_file]
for acce in my_dict:
date_time_str = str(acce['time'])
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M: %S.%f')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
##print(datetime.datetime.utcfromtimestamp(acce['time']).strftime('%Y-%m-%d %H:%M:%S'))
Which is giving me error
$python mytest4.py
File "mytest4.py", line 11, in <module>
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:
%S.%f')
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '1540532851987' does not match format '%Y-%m-%d
%H:%M:%S.%f'
EDIT: None of the threads mentioned deals with the following together 1. Multiple timestamps received as Json object in UTC format. 2. The format contains 13 digits, unlike the examples discussed with lesser number of digits. 3. Found a thread based on object_hook but it is too complecated for me.
Finally I could workout with the partial help of another thread.
I am sharing the final code here in case anyone need it.
import json
import datetime
with open("acc.json") as data_file:
my_dict = [json.loads(line) for line in data_file]
for acce in my_dict:
epoch_time = str(acce['time'])
# get first 10 digits
leading = str(epoch_time)[0: 10]
# get last 3 digits
trailing = str(epoch_time)[-3:]
# print timestamp with milliseconds
print datetime.datetime.fromtimestamp(float(leading)).strftime('%m/%d/%Y -- %H:%M:%S.') + ('%s' % int(trailing))
OUTPUT:
10/26/2018 -- 11:17:31.987
10/26/2018 -- 11:17:32.88
10/26/2018 -- 11:17:32.191