Identify whether the data is continuous in python

Asked 2 years ago, Updated 2 years ago, 22 views

I have a text file with the epoch time listed in ascending order for each line, and I want to see if the epoch time of each line is increasing at equal intervals like every minute. Do you have a Python library to identify if the data in the text file is continuous?

python

2022-09-30 20:11

2 Answers

You can convert from a string representing the date and time in strptime() to datetime.For example:

 from datetime import datetime
t=datetime.strptime('2014/01/01 00:01:02', '%Y / %m / %d %H: %M: %S')

Replace the first argument of strptime (part of '%Y / %m / %d %H: %M: %S') with the date and time extracted from one line of the text file

Find the difference in the data time taken from each of the upper and lower rows, and if the difference is the same between all rows, it means equal spacing.


2022-09-30 20:11

Assume that the epoch time points to the formal elapsed seconds from 12:0:0:0 AM on January 1, 1970, so-called UNIX Time.This means that the accuracy is seconds, so you just have to check if the number is increasing by 60 for each line.

As far as I know, at least there is no such library.

I don't know the format of the text file, but if it's a single column file, I think I can achieve my goal with the code below.

t.py:

def validate(cur,prev):
  if previs None —return None # default
  return cur==(prev+60)

with open('t.csv', 'r') as f:
  prev = None
  for n, line enumerate (f,1):
    epoch=int(l,10)
    is_valid=validate(epoch,prev)
    print('{}\t{}\t{}.format(n,epoch,is_valid))
    prev=epoch

t.csv:

1527122450
1527122510
1527122550

execute and result:

python t.py

1 1527122450 None
2 1527122510 True
3 1527122550 False


2022-09-30 20:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.