Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

My original time is the string in this format:

2017-01-22 13:54:55

But in excel after opening the file, my date format becomes:

01/22/17 13:54:55

I tried to parse it as follows:

 try:

                return dt.strptime('01/22/17 13:54:55', '%Y-%m-%d %H:%M:%S')

            except Exception as e:

                try:

                    return dt.strptime('01/22/17 13:54:55', '%-m/%-d/%y %H:%M:%S')

                except Exception as e:

                    raise ValueError('Reference time must be the format of: %Y-%m-%d %H:%M:%S, or a datetime object')

Is there the way to parse "01/22/17 13:54:55" by the strptime function?

1 Answer

0 votes
by (36.8k points)
edited by

Yeah, remove a - instead:

dt.strptime('01/22/17 13:54:55', '%m/%d/%y %H:%M:%S')

Master end-to-end Data Science through Data Science Online Courses 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...