0

I have this string:

Sat Apr 18 23:22:15 PDT 2009

and I want to extract

23

what should I have for it ? something like \d\w

CFD
  • 607
  • 1
  • 11
  • 29
  • 4
    Have you tried using `datetime`'s `strptime()`? https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior – liamhawkins Feb 08 '19 at 21:09
  • Is the string just a string, or are you using a function to get the string? – Trooper Z Feb 08 '19 at 21:11
  • just a string @TrooperZ – CFD Feb 08 '19 at 21:13
  • You could always just string slice `value[11:13]` will yield `23` assuming you've defined the string as `value`. If you have an entire column of these strings in say a dataframe, you could make a new column `df['hour'] = df['value'][11:13]` – atlas Feb 08 '19 at 21:16

5 Answers5

2

Use datetime to parse datetime strings, then you can easily extract all the parts individually

from datetime import datetime

dtime = datetime.strptime('%a %b %d %H:%M:%S %Z %Y', 'Sat Apr 18 23:22:15 PDT 2009')

hour = dtime.hour
year = dtime.year
# etc.

See docs for more details:

liamhawkins
  • 1,301
  • 2
  • 12
  • 30
1

You could use re.split to split on either spaces or colons and grab the 4th element:

import re

somedate = "Sat Apr 18 23:22:15 PDT 2009"

re.split('\s|\:', somedate)
['Sat', 'Apr', '18', '23', '22', '15', 'PDT', '2009']

hour = re.split('\s|\:', somedate)[3]

You could unpack it that way, as well:

day_of_week, month, day_of_month, hour, minute, second, timezone, year = re.split('\s|\:', somedate)

That would allow you more access

Otherwise, I'd go with @liamhawkins suggestion of the datetime module

EDIT: If you're looking for similar access paradigms to datetime objects, you can use a namedtuple from the collections module:

from collections import namedtuple

date_obj = namedtuple("date_obj", ['day_of_week', 'month', 'day_of_month', 'hour', 'minute', 'second', 'timezone', 'year'])

mydatetime = date_obj(*re.split('\s|\:', somedate))

hour = mydatetime.hour
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
0

Hope this will find the date in string and returns date

def get_date(input_date): date_format = re.compile("[0-9]{2}:[0-9]{2}:[0-9]{2}") date_search =date.search(input_date) if date_search: date = date_search.group() if date:
return date[:2] return ''

paras chauhan
  • 777
  • 4
  • 13
0

While this could be accomplished with re, the use of datetime.strptime in @liamhawkins answer [ https://stackoverflow.com/a/54600322/214150 ] would be preferred, assuming you are always dealing with formatted dates.

In addition, you could accomplish your goal by simply using a string method (.split()) and basic slicing of the resulting list. For example:

import re

word = 'Sat Apr 18 23:22:15 PDT 2009'

# Example using re.
rehour = re.findall('(\d+):\d+:\d+', word)
print('rehour:', *rehour)

# Example using string.split() and slicing.
somedate = word.split(' ')
somehour = somedate[3][:2]
print('somedate:', somedate)
print('somehour:', somehour)
Chris Larson
  • 1,684
  • 1
  • 11
  • 19
0

if it is truly just a string and the data you want will always be at the same position you could just do this.

String = "Sat Apr 18 23:22:15 PDT 2009"
hour = String[11:13]
print(hour)

This returns,

23

This works the same even if its from datetime or something.

If this is some other output from a function you can just convert it to a string and then extract the data the same way.

hour = str(some_output)[11:13]

If however you are not sure the data you want will always be in the same place of the string then I would suggest the following.

import re

somestring = "More text here Sat Apr 18 23:22:15 PDT 2009 - oh boy! the date could be anywhere in this string"

regex = re.search('\d{2}\:\d{2}\:\d{2}', somestring)
hour = regex.group()[:2]

print(hour)

the regex.group() is returning,

23:22:15

And then [:2] is extracting the first two items to return,

23