Brian Andrews
Brian Andrews

Reputation: 1

Python sorting timestamp

I am struggling with something that should be relatively straight forward, but I am getting nowhere.

I have a bunch of data that has a timestamp in the format of hh:mm:ss. The data ranges from 00:00:00 all 24 hours of the day through 23:59:59.

I do not know how to go about pulling out the hh part of the data, so that I can just look at data between specific hours of the day.

I read the data in from a CSV file using:

with open(filename) as csvfile:
  reader = csv.DictReader(csvfile)
  for row in reader:
    time = row['Time']

This gives me time in the hh:mm:ss format, but now I do not know how to do what I want, which is look at the data from 6am until 6pm. 06:00:00 to 18:00:00.

Upvotes: 0

Views: 906

Answers (2)

Rob Watts
Rob Watts

Reputation: 7146

With the times in 24 hour format, this is actually very simple:

'06:00:00' <= row['Time'] <= '18:00:00'

Assuming that you only have valid timestamps, this is true for all times between 6 AM and 6 PM inclusive.

If you want to get a list of all rows that meet this, you can put this into a list comprehension:

relevant_rows = [row for row in reader if '06:00:00' <= row['Time'] <= '18:00:00']

Update:

For handling times with no leading zero (0:00:00, 3:00:00, 15:00:00, etc), use split to get just the part before the first colon:

> row_time = '0:00:00'
> row_time.split(':')
['0', '00', '00']
> int(row_time.split(':')[0])
0

You can then check if the value is at least 6 and less than 18. If you want to include entries that are at 6 PM, then you have to check the minutes and seconds to make sure it is not after 6 PM.

However, you don't even really need to try anything like regex or even a simple split. You have two cases to deal with - either the hour is one digit, or it is two digits. If it is one digit, it needs to be at least six. If it is two digits, it needs to be less than 18. In code:

if row_time[1] == ':': # 1-digit hour
    if row_time > '6': # 6 AM or later
        # This is an entry you want
else:
    if row_time < '18:00:00': # Use <= if you want 6 PM to be included
        # This is an entry you want

or, compacted to a single line:

if (row_time[1] == ':' and row_time > '6') or row_time < '18:00:00':
    # Parenthesis are not actually needed, but help make it clearer

as a list comprehension:

relevant_rows = [row for row in reader if (row['Time'][1] == ':' and row['Time'] > '6') or row['Time'] < '18:00:00']

Upvotes: 1

wdow
wdow

Reputation: 1

You can use Python's slicing syntax to pull characters from the string.

For example:

time = '06:05:22'

timestamp_hour = time[0:2] #catch all chars from index 0 to index 2

print timestamp_hour

>>> '06'

should produce the first two digits: '06'. Then you can call the int() method to cast them as ints:

hour = int(timestamp_hour)

print hour

>>> 6

Now you have an interger variable that can be checked to see if is between, say, 6 and 18.

Upvotes: 0

Related Questions