Datetime from string doesn't match - python

I am trying to match a specific datetime format from a string but I am receiving a ValueError and I am not sure why. I am using the following format:
t = datetime.datetime.strptime(t,"%b %d, %Y %H:%M:%S.%f Eastern Standard Time")
which is an attempt to match the following string:
Nov 19, 2017 20:09:14.071360000 Eastern Standard Time
Can anyone see why these do not match?

From the docs we can see that %f expects:
Microsecond as a decimal number, zero-padded on the left.
The problem with your string is that you have a number that's zero-padded on the right.
Here is one way to fix your issue:
new_t = t.partition(" Eastern Standard Time")[0].rstrip('0') + ' Eastern Standard Time'
print(new_t)
#Nov 19, 2017 20:09:14.07136 Eastern Standard Time
t2 = datetime.datetime.strptime(new_t,"%b %d, %Y %H:%M:%S.%f Eastern Standard Time")
print(t2)
#datetime.datetime(2017, 11, 19, 20, 9, 14, 71360)

As noted by pault and the documentation, the issue is that the %f directive is essentially limited to 6 decimal places for your microseconds. While their solution works fine for your string, you might have an issue if your string is something like
'Nov 19, 2017 20:09:14.071360123 Eastern Standard Time'
Because calling rstrip('0') in that case would not cut the microseconds to the proper length. You could otherwise do the same with regex:
import re
import datetime
date_string = 'Nov 19, 2017 20:09:14.071360123 Eastern Standard Time'
# use a regex to strip the microseconds to 6 decimal places:
new_date_string = ''.join(re.findall(r'(.*\.\d{6})\d+(.*)', date_string)[0])
print(new_date_string)
#'Nov 19, 2017 20:09:14.071360 Eastern Standard Time'
t = datetime.datetime.strptime(new_date_string,"%b %d, %Y %H:%M:%S.%f Eastern Standard Time")
print(t)
#datetime.datetime(2017, 11, 19, 20, 9, 14, 71360)

Related

How to strip a date string to datetime? [duplicate]

I'm trying to parse date in python. My code work with all other months except Sept
Here is my code
time.strptime("Sept. 30, 2014", "%b. %d, %Y")
I get this error
ValueError: time data 'Sept. 30, 2014' does not match format '%b. %d, %Y'
The abbreviation for September is Sep, not Sept.
>>> datetime.strptime("Sep. 30, 2014", "%b. %d, %Y")
datetime.datetime(2014, 9, 30, 0, 0)
Here's the list of all abbreviated month names for the en_US local.
The use of "Sept" in place of "Sep" seems to be a common occurrence. As suggested by #morgan-thrapp, you have to replace the former with the latter:
time.strptime("Sept. 30, 2014".upper().replace("SEPT", "SEP"), "%b. %d, %Y")
The use of upper() converts all lower-case characters to upper-case, so that the use of replace is more straightforward.

Don't understand this ValueError parsing a date string [duplicate]

This question already has answers here:
Parse hours without leading zeroes by strptime in Python
(3 answers)
Closed 4 years ago.
For some reason python is not parsing my date properly but I looked at the strftime/strptime behavior and it looks to be right
import time
d = 'May 17, 2018 3:10 AM PDT'
time.mktime(time.strptime(d, "%B %d, %Y %I:%M %p %Z"))
If I do:
time.strftime("%B %d, %Y %I:%M %p %Z")
I get May 18, 2018 02:47 PM EDT, which looks to be the exact same format except for the leading 0 but strptime should be able to parse leading 0s.
What am I doing wrong in parsing this date?
Edit: Found out its the timezone but not sure why:
time.mktime(time.strptime("May 17, 2018 3:10 AM UTC", "%B %d, %Y %I:%M %p %Z"))
returns a value
time.mktime(time.strptime("May 17, 2018 3:10 AM PDT", "%B %d, %Y %I:%M %p %Z"))
returns ValueError
Python date handling has always been a little light in the timezone handling department (it's a complicated problem). You can implement your own derived tzinfo class from the abstract base class provided in the standard library if you only have a small subset of them that need to be handled—I've done it before and it's not too hard—or you can use something like the third-party dateutil module recommended in the documentation at the end of the tzinfo section which handles a much larger number of them.
Anyway, you can get dateutil from here or you can simply install it from an OS command line with pip install py-dateutil.
from dateutil import parser
t = parser.parse('May 17, 2018 3:10 AM PDT')
print('t: {!r}'.format(t)) # -> t: datetime.datetime(2018, 5, 17, 3, 10)

Converting string in python to date format

I'm having trouble converting a string to data format. I'm using the time module to convert a string to the YYYY-MM-DD format. The code below is what I've tried but I get the following error.
sre_constants.error: redefinition of group name 'Y' as group 5; was group 3
Here is the code
import time
review_date = "April 18, 2018"
review_date = time.strptime(review_date, '%m %d %Y %I:%Y%m%d')
Firstly, the error is because you're using %Y, %m, and %d twice in your time.strptime() call.
Secondly, you're using the wrong format. The format you pass to strptime() has to match the format of the date / time string you pass, which in this case is: %B %d, %Y.
This is a good reference on the different format types.
I normally use datetime for this:
from datetime import datetime
review_date = "April 18, 2018"
review_date = datetime.strptime(review_date, '%B %d, %Y').strftime('%Y-%m-%d')
This code returns review_date = '2018-04-18'. See https://docs.python.org/3/library/datetime.html
The date format for April is %B. strptime() converts to a datetime object, .strftime() converts the datetime object to a string.
time.strptime() is for parsing strings into date/time structures. It takes two arguments, the string to be parsed and another string describing the format of the string to be parsed.
Try this:
time.strptime("April 18, 2018", "%B %d, %Y")
... and notice that "%B %d, %Y" is:
Full locale name of the month ("April")
[Space]
Date of the month (18)
[Comma]
[Space]
Four digit year (2018)
The format string specification that you provided bears no resemblance to the formatting of your date string.
These "magic" formatting codes are enumerated in the documentation for time.strftime()
review_date = time.strptime(review_date, '%B %d, %Y')
import time
review_date = "April 18, 2018"
review_date = time.strptime(review_date, '%B %d, %Y')
That's what you should have

Python Convert unusual date string to datetime format

I have date string like this:
Saturday, 30 Nov, 2013
So it is like Day_Name, Day, Month_Name_3_Letters, Year.
I wonder what is the best way to convert it to datetime format using python?
I using like this:
datetime.strptime((row[7].split(',')[1] + row[7].split(',')[2]).replace(' ',''), "%d%b%Y").strftime("%Y-%m-%d")
Use strptime:
import datetime as dt
s = 'Saturday, 30 Nov, 2013'
d = dt.datetime.strptime(s,'%A, %d %b, %Y')
Result:
>>> d
datetime.datetime(2013, 11, 30, 0, 0)
As you'll see from the reference:
%A Weekday as locale’s full name.
%d Day of the month as a zero-padded decimal number.
%b Month as locale’s abbreviated name.
%Y Year with century as a decimal number.
You can use strptime function and initialize it as the following:
from datetime import datetime
datetime_object = datetime.strptime('Saturday, 30 Nov, 2013', '%A, %d %b, %Y')
print datetime_object
Conversely, the datetime.strptime() class method creates a datetime
object from a string representing a date and time and a corresponding
format string. datetime
In order to see how to use the formats and when, you can see strftime formats
Why don't you use dateutil's parse ?
from dateutil import parser
parser.parse('Saturday, 30 Nov, 2013')
datetime.datetime(2013, 11, 30, 0, 0)
from datetime import datetime
st='Saturday, 30 Nov, 2013'
print datetime.strptime(st,'%A, %d %b, %Y')
OUTPUT
2013-11-30 00:00:00
See strptime() at Tutorials point

Convert a datetime object

In Python how do I convert a datetime object like datetime.datetime(2010, 12, 18, 22, 21, 45, 277000) to something like "Sun Dec 18 2010 22:21:45 GMT+0000 (UTC)"?
You can use strftime for this purpose. Read more here.
Not sure about utc offset, but seems that you can use something like this:
import datetime
d = datetime.datetime(2010, 12, 18, 22, 21, 45, 277000)
d.strftime("%a %b %d %Y %H:%M:%S %z")
>>> Sat Dec 18 2010 22:21:45
Where using the link above:
%a - Locale’s abbreviated weekday name
%b - Locale’s abbreviated month name
%d - Day of the month as a decimal number [01,31]
%H - Hour (24-hour clock) as a decimal number [00,23]
%M - Minute as a decimal number [00,59]
%S - Second as a decimal number [00,61]
%z - UTC offset in the form +HHMM or -HHMM (empty string if the the
object is naive)

Categories