This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
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
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)
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 ''
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
Related
For a course I am taking, we have a tab delimited flat file that I am importing into Python 3 using a Jupyter Notebook. The format of the date time is below and again it is currently a text string.
Sat Jul 25 04:43:04 +0000 2020
How do I change this from text to a date/time in Python 3?
How do I change the date/time format to be: YYYY-MM-DD HH:MM?
Thanks,
Use the datetime standard module:
https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
https://docs.python.org/3/library/datetime.html#datetime.datetime.strftime
Example:
import datetime
s = 'Sat Jul 25 04:43:04 +0000 2020'
dt = datetime.datetime.strptime(s, '%a %b %d %H:%M:%S %z %Y')
string = dt.strftime('%Y-%m-%d %H:%M') # -> 2020-07-25 04:43
from datetime import *
test = 'Sat Jul 25 04:43:04 +0000 2020'
year = test[-4:]
month = test[4:7]
day = test[8:10]
time = test[12:19]
adjusted_date = f'{day}-{month}-{year}-{time}'
x = datetime.strptime(adjusted_date, "%d-%b-%Y-%H:%M:%S")
z = datetime.strftime(x, "%d-%m-%Y-%H:%M")
print(z)
OUTPUT:
2020-07-25 04:43
The easiest way I could think about right now. Hope that helps and answers your question.
Before anyone complains. User asking question is definitely new to programming. Reading docs etc it is like a black magic when you just started, all this %Y%M%D means nothing :D so above it is (imo) the most easy to understand way of doing it, self-explanatory.. and if someone is struggling with slicing.. then it should go back and learn it.
I want to extract the year month day hours min eachly from below value.
import os, time, os.path, datetime
date_of_created = time.ctime(os.path.getctime(folderName))
date_of_modi = time.ctime(os.path.getmtime(folderName))
Now I only can get like below
'Thu Dec 26 19:21:37 2019'
but I want to get the the value separtly
2019 // Dec(Could i get this as int??) // 26
each
I want to extract each year month day each time min value from date_of_created and date_of_modi
Could i get it? in python?
You can convert the string to a datetime object:
from datetime import datetime
date_of_created = datetime.strptime(time.ctime(os.path.getctime(folderName)), "%a %b %d %H:%M:%S %Y") # Convert string to date format
print("Date created year: {} , month: {} , day: {}".format(str(date_of_created.year),str(date_of_created.month),str(date_of_created.day)))
The time.ctime function returns the local time in string form. You might want to use the time.localtime function, which returns a struct_time object which contains the information you are looking for. As example,
import os, time
date_created_string = time.ctime(os.path.getctime('/home/b-fg/Downloads'))
date_created_obj = time.localtime(os.path.getctime('/home/b-fg/Downloads'))
print(date_created_string) # Mon Feb 10 09:41:03 2020
print('Year: {:4d}'.format(date_created_obj.tm_year)) # Year: 2020
print('Month: {:2d}'.format(date_created_obj.tm_mon)) # Month: 2
print('Day: {:2d}'.format(date_created_obj.tm_mday)) # Day: 10
Note that these are integer values, as requested.
time.ctime([secs])
Convert a time expressed in seconds since the epoch to a string of a form: 'Sun Jun 20 23:21:05 1993' representing local time.
If that's not what you want... use something else? time.getmtime will return a struct_time which should have the relevant fields, or for a more modern interface use datetime.datetime.fromtimestamp which... returns a datetime object from a UNIX timestamp.
Furthermore, using stat would probably more efficient as it ctime and mtime will probably perform a stat call each internally.
You can use the datetime module, more specifically the fromtimestamp() function from the datetime module to get what you expect.
import os, time, os.path, datetime
date_of_created = datetime.datetime.fromtimestamp(os.path.getctime(my_repo))
date_of_modi = datetime.datetime.fromtimestamp(os.path.getmtime(my_repo))
print(date_of_created.strftime("%Y"))
Output will be 2020 for a repo created in 2020.
All formats are available at this link
I am trying to find best way to convert "Friday 1st March 2019" or "Saturday 2nd March 2019" to python datetime object.
I tried by splitting, than had thoughts on regex, but I am quite sure there is more 'elegant' way ofdoing it
From string "Friday 1st March 2019" I expect 01-03-2019 or 2019-03-01
TNX!
Maybe not the "best" way, but a very easy way is dateutil's parser
from dateutil import parser
parser.parse("Friday 1st March 2019")
Returns:
datetime.datetime(2019, 3, 1, 0, 0)
It can pretty much be wrapped up as:
from dateutil import parser
from datetime import datetime as dt
dt.strftime(parser.parse("Friday 1st March 2019"), "%d-%m-%Y")
Returning:
'01-03-2019'
Please refer to an already answered question:
How to get the datetime from a string containing '2nd' for the date in Python?
As I can only repeat, solution is to use dateutil parser:
from dateutil.parser import parse
your_string = "Friday 1st March 2019"
date_obj = parse(your_string)
Behind the scenes, I guess the "1st", and "2nd" parts are extracted somehow (e.g. splitting+regex) and simplified to its day value only.
According to an input like that, the common datetime library can be used with proper date format string:
import datetime
simplified_txt = "Friday 1 March 2019"
datetime_obj = datetime.datetime.strptime(simplified_txt,"%A %d %B %Y")
You are going to face issues with 1st, 2nd.
So, try this (without adding any external/third party library):
import re
from datetime import datetime as dt
ds = "Friday 1st March 2019"
parts = ds.split(" ")
ds = "{} {} {} {}".format(
parts[0],
re.sub('[^0-9]','', parts[1]),
parts[2],
parts[3]
)
a = dt.strptime(ds, "%A %d %B %Y")
If you want to make it into a function, do this:
def convdate(s):
parts = s.split(" ")
ds = "{} {} {} {}".format(
parts[0],
re.sub('[^0-9]','', parts[1]),
parts[2],
parts[3]
)
return dt.strptime(ds, "%A %d %B %Y")
I know there are similar questions out there, but most simply convert the alphanumerical date string to a datetime object with the strptime method, which is not what I'm going for. All I'm trying to do is convert string like so.
Format of Possible Input
December 7, 2015
October 24, 2018
Desired Output
2015-12-07
2018-10-24
How I've Gone About It
""" funding_expiration[0] is equal to strings like 'December 7, 2015' """
funding_expiration = funding_expiration[0].text.split()
""" still need to convert Month (%B) to ## (%m) """
funding_expiration[1] = funding_expiration[1].replace(',', '')
# add padding to single digit days
if len(funding_expiration[1]) is 1:
funding_expiration[1] = funding_expiration[1].zfill(1)
# format numerical date string
funding_expiration = funding_expiration[2] + '-' + funding_expiration[0] + '-' + funding_expiration[1]
I'm still trying to figure out an efficient way to convert the full name of months into their corresponding numerals. I'm new to Python, so I'm wondering whether or not there's a more efficient way to accomplish this?
datetime.strptime can work in your case too. You can use the %B directive to parse full month names.
import datetime
s = 'December 7, 2015'
date_string = str(datetime.datetime.strptime(s, '%B %d, %Y').date())
>>> date_string
'2015-12-07'
Here's a solution using 3rd party dateutil:
from dateutil import parser
L = ['December 7, 2015', 'October 24, 2018']
res_str = [parser.parse(x).strftime('%Y-%m-%d') for x in L]
['2015-12-07', '2018-10-24']
How can I get Python to output
'Mon Jun 04'
'Tue Jun 05'
etc, for a week of given time?
ex
today = datetime.datetime.today()
### do some magic
days = ['Tue Jun 05',...]
What do I do with 'today' to generate the results? I'm not even sure if I'm the right module, calender seems to share similar features.
days = [today.strftime("%a %b %y"), ...]
We use strftime to take a datetime object and format it to a string
http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior
Look at the strftime method of datetime objects.