Problems in converting string to datetime object with strptime - python

I'm trying to convert a time string into a datetime object with strptime. The problem is that I'm getting a format error from string to datetime object.
I don't understand why this format is not appropriate for my data.
import numpy as np
from datetime import datetime
Vent_date = np.array([b'"2018-06-28 15:00:00"', b'"2018-06-28 15:00:00"'], dtype='|S21')
dates = []
for line in Vent_date:
line1 = line.decode('utf-8')
dates.append(datetime.strptime(line1,'%Y-%m-%d %H:%M:%S'))
I get:
ValueError: time data '"2018-06-28 15:00:00"' does not match format '%Y-%m-%d %H:%M:%S'

If you notice the error contains double quotes wrapped in single quotes. So it looks like your source data has double quotes in it which is why it is failing.
A few simple solutions:
Fix (remove) the quotes in your source data and use your original code
Strip the quotes from the string before trying to parse it:
dates.append(datetime.strptime(line1.strip('"'),"%Y-%m-%d %H:%M:%S"))
Change your date format string to look for a date containing double quotes:
dates.append(datetime.strptime(line1,'"%Y-%m-%d %H:%M:%S"'))
Use Python's csv library which may handle reading csv files better

Related

Convert date string to Datetime Python Format Error

I know there are a lot of answers to this question online, but none of them have worked for me. I am trying to convert a date string into a Datetime object, of the following format: yyyy-mm-dd
My date_string is '2017-02-02T00:00:00Z'
I am trying to convert it by doing date_value = datetime.datetime.strptime(date_string, '%Y%m%d') but I'm getting the following error:
ValueError: time data '"2017-02-02T00:00:00Z"' does not match format
'%Y%m%d'
Also, should I be worried about the double quotes around my date_string string?
The second argument in the method strptime is the pattern of your string.
Here is the full list of available code formats. https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes
All the remaining "non-informative" characters in your string can simply be put as-is in there correct places.
Thanks to #MrFuppes for this info: you should also parse the trailing "Z" as %z. This will signal python that it's a UTC datetime and not a local datetime.
Your code should be :
date_string = '2017-02-02T00:00:00Z'
date_value = datetime.datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S%z')
As for the extra quotes, that's not wanted. You should try this beforehand :
date_string = date_string.strip("'").strip('"')
If strip() didn't work, you can call eval instead (usually not recommended) :
date_string = eval(date_string)
The solution is to parse your date_string first, and that should help. Using strptime() right away on an unparsed datetime string can sometimes cause problems. Also you shouldn't worry about your double quotes, it's fine.
First, install the python-dateutil library if you haven't already (pip install python-dateutil at the command line). Then test the solution with the following code.
import datetime
import dateutil.parser
date_string = '2017-02-02T00:00:00Z'
#we parse the string, it becomes a datetime object
parsed_date_string = dateutil.parser.parse(date_string)
print(parsed_date_string)
#output looks like this: 2017-02-02 00:00:00+00:00
#now your statement will work
date_value = datetime.datetime.strptime(str(parsed_date_string), '%Y-%m-%d %H:%M:%S%z')
print(date_value)
#output will also be: 2017-02-02 00:00:00+00:00
The strptime() statement worked this time because we parsed our date first with parse(). Note also that to use strptime() we need to cast our parsed_date_string back to a string because parse() converts our original string to an object of class datetime.datetime and strptime() is expecting a string.
Hopefully that helped.

Python: convert string to datetime object failed if the format is with comma

So i try to convert my string to date time object without knowing the format this way:
date = '019-03-13 17:35:35.855'
date_object = datetime.fromisoformat(date)
So this works fine but in case the datetime object failed if the format is with comma this fail:
date = '019-03-13 17:35:35,855'
date_object = datetime.fromisoformat(date)
ValueError: Invalid isoformat string: '2019-03-13 17:35:35,855'
And most of my files written with this comma format.
Any suggestions ?
datetime.fromisoformat expects you to send a string in a particular format:
Specifically, this function supports strings in the format(s) YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]], where * can match any single character.
If your format differs, you should use strptime and set your format in it. If you have no single format or you have dirty data, the only way you can to process it is to clean it first with some kind of data refining function.

Input format to convert with datetime.strptime

I am trying to convert a string with "2019-01-06T01:00:24.908821" to a date using the "datetime.strptime" function. However, I am not able to find the format for this conversion to be successful.
I'm using the entries as proposed by the library itself, however I'm getting a "ValueError" when I try to perform the conversion.
ValueError: time data '2019-01-06T01:00:24.908821' does not match format '%Y-%m-%dT%H:%M:%S:%f'
If you would like to read the proposed standards, you can find here:
https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
My code:
from datetime import datetime
datetime.strptime("2019-01-06T01:00:24.908821", "%Y-%m-%dT%H:%M:%S:%f")
You have a colon (:) instead of a decimal (.) before the %f in your format string.
Change
datetime.strptime("2019-01-06T01:00:24.908821", "%Y-%m-%dT%H:%M:%S:%f")
To
datetime.strptime("2019-01-06T01:00:24.908821", "%Y-%m-%dT%H:%M:%S.%f")

Unable to parse the strptime in python

I am converting the datetime into time. My JSON datetime format is "2017-01-02T19:00:07.9181202Z". I have placed my code below:
from datetime import datetime
date_format = datetime.strptime('2017-01-02T19:00:07.9181202Z', '%Y-%m-%dT%H:%M:%S.%fZ')
time = date_format.strftime("%I:%M %p")
print(time)
Error message as below:
After that I read this python date-time document. It says that microsecond digit should be 6. But, JSON date-time microsecond has 7 digit.
Message from Python document:
%f is an extension to the set of format characters in the C standard
(but implemented separately in datetime objects, and therefore always
available). When used with the strptime() method, the %f directive
accepts from one to six digits and zero pads on the right.
I need result like 07:00 PM format. Is there any alternative method?
Thanks in advance.
If you're sure that the input will always be like that, you can just remove the extra digit before passing that string to strptime:
date_format = datetime.strptime('2017-01-02T19:00:07.9181202Z'[:-2] + 'Z', '%Y-%m-%dT%H:%M:%S.%fZ')
This is dirty, but gives the idea - remove the last two characters (the extra digit and "Z"), re-add the "Z".

change date format in django

I'me trying to use this eventCalendar in Django, which saves and shows dates in this format:
2012-02-27T13:15:00.000+10:00
but when I save events in the database, they're saved in this format:
Mon Feb 27 2012 13:15:00 GMT+0330 (Iraq Standard Time)
so events from the database won't appear on the calendar because of this format. How can I convert this format?
I tried some thing like this:
datetime.strptime(mydatetime, "%Y-%m-%dT%H:%M:%S+0000")
but I'm repeatedly getting errors like this:
'module' object has no attribute 'strptime'
Edit:date is in string format
strptime is used to parse a string into a datetime object. The format string indicates how to parse the string, not the format you want the datetime to take when later printed as a string. So first off you need to make the format string match the date format of the input string.
Once you've gotten a datetime from strptime, you can then use strftime with your current format string to get it into the display you want.
That said, though, it appears you've got a problem with your imports. The error seems to indicate that you've done:
import datetime
datetime.strptime(...)
That's incorrect. strptime and strftime are methods off datetime.datetime, so you need to either modify your import like:
from datetime import datetime
Or, modify your call to strptime like:
datetime.datetime.strptime(...)
UPDATE
You're starting off with a string like Mon Feb 27 2012 13:15:00 GMT+0330 (Iraq Standard Time). Python is pretty awesome, but it's not omniscient; if you want to convert this to a datetime you have to tell it how. That's the purpose of the format string you pass to strptime. You need to create a format string that represents your current string date and time as represented in the database (exercise left to reader). Think in reverse, along the lines of it you wanted to actually represent a datetime like that, how would you do it.
This will net you a datetime. From there, you can now format that datetime as a string with strftime, passing the actual format you want, this time.
So the process is:
Create a format string representing your current string from the database
Use that format string as an argument to strptime to get a datetime
Create a format string representing the format you want the date to be in (already done)
Use that format string as the argument to strftime to convert the datetime from step 2 to your desired string.

Categories