Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python strptime format with optional bits

Right now I have:

timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f') 

This works great unless I'm converting a string that doesn't have the microseconds. How can I specify that the microseconds are optional (and should be considered 0 if they aren't in the string)?

like image 508
Digant C Kasundra Avatar asked Jun 01 '15 22:06

Digant C Kasundra


People also ask

What is the difference between Strptime and Strftime?

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.

How does Strptime work in Python?

The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it. It raises ValueError if the string cannot be formatted according to the provided directives.

How do I use Strftime and Strptime in Python?

Python time strptime() MethodThe format parameter uses the same directives as those used by strftime(); it defaults to "%a %b %d %H:%M:%S %Y" which matches the formatting returned by ctime(). If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised.

What does Strptime return?

Return Value On successful completion, the strptime() function returns a pointer to the character following the last character parsed. Otherwise, a null pointer is returned.

How strptime () works in Python?

How strptime () works? This function takes two arguments, a string in which some time is given and a format code, to change the string into, the string is changed to the DateTime object as per the list of codes given below. 1, 2..

What is strptime () method in Java?

The strptime() method creates a datetime object from a given string. Note: You cannot create datetime object from every string. The string needs to be in a certain format.

How do I get a ValueError in strptime ()?

A literal '%' character. If the string (first argument) and the format code (second argument) passed to the strptime () doesn't match, you will get ValueError. For example: If you run this program, you will get an error.

How to build a format string without splitting the time string?

To build a format string without splitting the time string and discarding extra text, just include the extra text in the format string. t [t.index (',',t.index (',') + 1):] is extra text.


2 Answers

You could use a try/except block:

try:     timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f') except ValueError:     timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S') 
like image 178
Alexander Avatar answered Sep 20 '22 07:09

Alexander


What about just appending it if it doesn't exist?

if '.' not in date_string:     date_string = date_string + '.0'  timestamp = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f') 
like image 34
stevieb Avatar answered Sep 23 '22 07:09

stevieb