-1

I am new to python. I was wondering how I could diff the two logcat timestamp intervals and the x and y coordinates from the below logcat msg.

I have a file with the following msg from the logcat where they are represented as follows

Date    Time(HH:MM::SS:MS)  ACTION_TYPE, (X,Y)                              
05-03 12:53:15.251 ACTION_MOVE, (596.00, 841.00) 
05-03 12:53:15.268 ACTION_MOVE, (599.00, 847.00) 

I would like to get the output as in the csv file

timestampdiff(millsec), x_change,y_change
17,3.00,6.00
Reto
  • 1,305
  • 1
  • 18
  • 32
chhajjer
  • 1
  • 2
  • have you tried anything? Do you know how to read a file in Python? stored in specified character encoding? line by line? Do you know regular expressions? Do you know how to parse a time string in a specified format? Do you know how to parse a string that contain a float number in Python? Do you know how to iterate pairwise in Python? Do you know how to write to a csv file a list of tuple such as `[(17, 3.0, 6.0)]`? – jfs May 05 '16 at 18:58
  • Do all lines in the log file have the format you described? If so you can easily split the lines on significant positions and convert the date-time-strings into a [https://docs.python.org/2/library/datetime.html](datetime) object and use `(date1 - date2).microseconds / 1000` for your milliseconds. – Simon Fromme May 05 '16 at 19:02

1 Answers1

0

You can use regex to parse the file (once you read it)

import re

# example string from your file
s='05-03 12:53:15.251 ACTION_MOVE, (596.00, 841.00) '

# The unescaped round brackets will capture the numbers 
r =re.match(r'\d+-\d+\s+\d+:\d+:(\d+\.\d+)\s+[A-Z_]+,\s+\(\d+\.\d+,\s+\d+\.\d+\)', s)

r.groups(0)  # Will output ('15.251',)

Repeat the above for all numbers that you need, convert to decimal (or float) and calculate the difference.

Community
  • 1
  • 1
Jaimes
  • 347
  • 3
  • 5
  • Thanks this works !! after using r = re.match(r'\d+-\d+\s+(\d+):(\d+):(\d+)\.(\d+)', s) and then seetting currtime = (int(r.group(1)) * 24 * 60 + int(r.group(2)) * 60 + int(r.group(3))) * 1000 + int(r.group(4)) outputs cuurent time in ms. – chhajjer May 05 '16 at 22:34