0

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
Bukaida
  • 225
  • 2
  • 6
  • 15
  • Appears to be unix epoch. https://www.epochconverter.com/ – dfundako Oct 26 '18 at 18:03
  • 2
    Possible duplicate of [Converting Epoch time into the datetime](https://stackoverflow.com/questions/12400256/converting-epoch-time-into-the-datetime) – dfundako Oct 26 '18 at 18:04
  • I have tried the code on windows platform also and the error here is print(time.ctime(acce['time'])) ValueError: (22, 'Invalid argument') – Bukaida Oct 26 '18 at 18:25
  • @dfundako That website is a nice one. It showed the timestamp meaning but I require to extract only min:sec from the stamp. I do not have enough knowledge in Python to achieve that. – Bukaida Oct 26 '18 at 18:44
  • Finally this thread https://stackoverflow.com/questions/49249080/can-not-convert-13-digit-unix-timestamp-in-python addresses my query partly. I have shared the final code in OP in case anyone need it. – Bukaida Oct 27 '18 at 05:39

1 Answers1

0

Here is my solution:

import datetime
import json
# {"sensor":"Accelerometer","time":1540532851987,"dataZ":"8.2821044921875"}
# {"sensor":"Accelerometer","time":1540532852088,"dataZ":"8.162399291992188"}
# {"sensor":"Accelerometer","time":1540532852191,"dataZ":"9.026702880859375"}
jsonString = {"sensor":"Accelerometer","time":1540532851987,"dataZ":"8.2821044921875"}
json_data = json.dumps(jsonString)

data = json.loads(json_data)
my_dict = [data]

for item in my_dict:
    rawTime = str(item.get('time'))
    firstHalf = rawTime[:10]
    secondHalf = rawTime[10:]
    formatTime = firstHalf + '.' + secondHalf
    convertedTime = datetime.datetime.fromtimestamp(float(formatTime)).strftime('%c')
    print(convertedTime)