简体   繁体   中英

Converting string to time in python is giving error

this question might be answered but I can't find link. I'm trying to convert string to time. My string looks like '9 am'

x = datetime.datetime.strptime(start,"%I:%M %p")


I get following error:

time data '4:30 pm' does not match format '%I:%M %p'

Where I'm wrong??

Your problem is that "pm" is not the same as "pm", and %p is looking only for "am" or "pm".

In order to convert your string start from "4 pm" to "4 pm", you can use

start = start.replace(".", "")

This replaces all "." characters in the string with nothing at all.

If you remove "." from your string before you convert it to a datetime, it should cause no problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM