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
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
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:
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
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 ''
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)
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