Convert Alphanumerical Date String to Numerical Date String - python

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']

Related

Parsing Dates In Python

I have a list of dates from input like the ones below.
I am working on a project and only want to accept the dates that follow the format April 1, 1990 or the January 13, 2003 (taking in any month) format from user input, and any other date format I do not want to accept. I am struggling on how I would use the replace or find function to obtain these goals? Once I receive that format I want to print out the date in this format 7/19/22. If I have the dates in the right format I used the replace function to replace the space, and comma but how would I take that month and replace it with its numerical value? Sorry for all these questions I am just stuck and have been working on this for a while now.
April 1, 1990
November 2 1995
7/19/22
January 13, 2003
userinput = input("Please enter date")
parsed_date = userinput.replace(" ", "/", 2)
new_parsed_date = parsed_date.replace(',',"")
print(new_parsed_date)
March/1/2019 Here is my output when I parse the date. Is there also any easier way to do this task?
You should take a look at the strptime method of the Python datetime object.
Basically you would write code that looks like this:
>>> from datetime import datetime
>>> datetime.strptime("January 13, 2003", "%B %d, %Y")
datetime.datetime(2003, 1, 13, 0, 0)
Documentation for strptime: https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime
I can't really understand what you're going to do. You said that you only want to accept the certain dates formats from user input and ignore other dates formats.
April 1, 1990 # Accept
January 13, 2003 # Accept
November 2 1995 # Ignore (lack of comma)
7/19/22 # Ignore (use numerical value in month field)
May I just think that you would like to accept the format like January 13, 2003 and print or save them in the format 01/13/2003?
Then you should consider strptime() and strftime() methods for datetime object.
# get the date string from user input
date_input = input("Please Enter Date: ")
input_format = "%B %d, %Y"
output_format = "%m/%d/%Y"
try:
parsered_date = datetime.datetime.strptime(date_input, input_format)
.strftime(output_format)
print(parsered_date)
except ValueError:
print("This is the incorrect date string format.")

how to get this regular expression in python [duplicate]

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

What is the difference between strptime and strftime?

I am working on a project , where I have dictionary with column date called "starttime" .. I need to extract month , hour and year , day of the week.
I am stuck for now I have the below code .
{if city =='NYC':
datn = datum[('starttime')]
datn = dt.strptime(datn,"%m/%d/%y %H:%M:%S")
hour = dt.strftime(datn,"%H")
year = dt.strftime(datn,"%y")
elif city == 'Chicago':
datc = datum[('starttime')]
datc = dt.strptime(datc,"%m/%d/%y %H:%M")
month = dt.strftime(datc,"%m")
hour = dt.strftime(datc,"%H")
year = dt.strftime(datc,"%y")
else:
datw = datum[('start date')]
datw = dt.strftime (datw,"%m")
hour = dt.strftime(datw,"%H")
year = dt.strftime(datw,"%y")
return (month, hour, day_of_week)
}
my import statements are on the top of my code , as below:
from datetime import datetime
strptime translates to
"parse (convert) string to datetime object."
strftime translates to
"create formatted string for given time/date/datetime object according to specified format."
Why do you need strftime?
This is what a datetime object looks like: (2015, 7, 19, 22, 7, 44,
377000)
To someone who isn't quite familiar with this format, with the
exception of the year, what's written up there is not immediately
intuitive. So you probably would be better off with something like
Sun, 19 July, 2015. That's what strftime is used for. You simply need
to learn the proper formatting strings.
One Good Link over SO read about this !
strptime converts the string to a datetime object.
strftime creates a formatted string for given time/date/datetime object according to specified format by the user
you would use strftime to convert a datetime object like this: datetime (2018, 10, 20, 10, 9, 22, 120401) to a more readable format like "20-10-2018" or 20th of October 2018.

Converting dates in Python

I have dates in the form 26/11/2015. How can I convert them into the format 26-Nov-2015 and still keep them as dates and not strings?
Your question does not make much sense. If you keep them as dates, they have no format. The format is only manifested when you convert them to strings.
So the answer is: Store the dates as date (or datetime) objects, and use datetime.strftime with some specific format whenever you need them as a string:
>>> from datetime import date
>>> d = date(2016, 11, 26)
>>> d.strftime("%Y/%m/%d")
'2016/11/26'
>>> d.strftime("%d-%b-%Y")
'26-Nov-2016'
Conversely, use strptime to parse strings in different formats to dates:
>>> datetime.datetime.strptime("26-Nov-2015", "%d-%b-%Y")
datetime.datetime(2015, 11, 26, 0, 0)
from datetime import datetime
date = datetime.strptime('26/11/2015', '%d/%m/%Y')
print date.strftime("%d-%B-%Y")
In the above example, we are taking your input string 'dd/mm/yyyy' and turning it into a python datetime saving it to a variable called date (for future usage as per your request), and then printing it out in the format requested.
You want to use the datetime module I think. For example:
from datetime import date
a = date(2015, 11, 26)
a.strftime("%A %d of %B, %Y")
should give you 'Thursday 26 of November, 2015'
Or for your specific formatting request:
a.strftime("%d-%b-%Y") #'26-Nov-2015'
Hope this helps, good luck!

Strings with start and end time to datetime

For a project I need to parse a few date strings. One date is given, which is the Monday of the week, for example: Feb 16, 2015.
Then, I have managed to extract a list of dates which looks like this:
['Wednesday', '11:00AM', '7:00PM']
['Friday', '10:15AM', '4:30PM']
['Sunday', '12:00AM', '5:00PM']
Where the first value is obviously the day, the second value is the starting time and the last value is the ending time (it's for events).
I would like to convert this to actual python datetime objects, but it must align with the starting date.
What I have tried so far is using this:
# Beginning of week
BoW = datetime.strptime('Feb 16, 2015', "%b %d, %Y")
# List of days
for day in days:
# Find the Day of the Week
# This outputs a number, 0 = Monday, 6 = Sunday
DoW = table.index(tr)
schedule.append({
'start' : BoW + timedelta(days=DoW),
'end' : BoW + timedelta(days=DoW)
})
As you can see, this will only successfully set the date, but not the hour/minute. Is there a way to easily achieve this?
You can parse out the time with strptime() as well:
timevalue = datetime.strptime(timecomponent, '%I:%M%p').time()
%I is a 12-hour clock hour value (so between 1 and 12) and %p is the AM or PM qualifier.
You can then use datetime.datetime.combine() to combine that time component with your date:
BoW = datetime.strptime('Feb 16, 2015', "%b %d, %Y").date()
start = datetime.strptime(start_time, '%I:%M%p').time()
end = datetime.strptime(end_time, '%I:%M%p').time()
start, end = datetime.combine(BoW, start), datetime.combine(BoW, end)
Demo:
>>> from datetime import datetime, timedelta
>>> BoW = datetime.strptime('Feb 16, 2015', "%b %d, %Y").date()
>>> datetime.strptime('11:00AM', '%I:%M%p').time()
datetime.time(11, 0)
>>> datetime.combine(BoW, _)
datetime.datetime(2015, 2, 16, 11, 0)
Not sure exactly what type of situation you are in, but you might try the cool NLP time parsing library parsedatetime. It would let you do something like this:
import parsedatetime
cal = parsedatetime.Calendar()
WoY_timetuple, status = cal.parse("Feb 16, 2015")
DoW_timetuple, status = cal.parse("Wednesday", sourceTime=WoY_timetuple)
start, status = cal.parseDT("11:00AM", sourceTime=DoW_timetuple)
end, status = cal.parseDT("7:00PM", sourceTime=DoW_timetuple)
So you can easily set the parsing context to get each time of day string into a full datetime object. You would need to make sure you carefully handle the status codes returned by the .parse method. If your input data has some variability in date formatting this approach could save time, otherwise Martijn's approach is probably a lot safer.

Categories