Skip to content

Instantly share code, notes, and snippets.

@okoto-xyz
Created October 27, 2019 15:32
Show Gist options
  • Save okoto-xyz/c8eead8d7eb8ffc01310abda267bec93 to your computer and use it in GitHub Desktop.
Save okoto-xyz/c8eead8d7eb8ffc01310abda267bec93 to your computer and use it in GitHub Desktop.
import re
from datetime import datetime
from datetime import timedelta
logFilePath = "debug.log"
outputFilePath = "output.csv"
with open(logFilePath, "r") as logFile, open(outputFilePath, "w") as csvFile:
lines = logFile.readlines()
csvFile.write("date,elapsed time,height\n")
startDatetime = None
_td = timedelta(0)
for line in lines:
match = re.match("^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}).* height=(\\d*) .*", line)
if match:
date = match.group(1)
dt = datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
if startDatetime is None:
startDatetime = dt
td = dt - startDatetime
if td >= _td:
height = match.group(2)
csvFile.write("{0},{1},{2}\n".format(date, td, height))
# output every 10 minutes
_td = _td + timedelta(minutes=10)
@cryptozeny
Copy link

cryptozeny commented Dec 7, 2019

i got error:

$ ./synctime.py 
from: can't read /var/mail/datetime
from: can't read /var/mail/datetime
./synctime.py: line 5: logFilePath: command not found
./synctime.py: line 6: outputFilePath: command not found
./synctime.py: line 8: syntax error near unexpected token `('
./synctime.py: line 8: `with open(logFilePath, "r") as logFile, open(outputFilePath, "w") as csvFile:'

i had to add this line at first on linux.

#!/usr/bin/env python

or this

python synctime.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment