“07:37 am GMT+5.30 on 10/11/2011”如何将此字符串转换为 python 中的日期时间

"07:37 am GMT+5.30 on 10/11/2011" how to convert this string to datetime in python

根据题目要求去掉字符串中的+5.30:

import datetime
b="07:37 am GMT+5.30 on 10/11/2011"
a=b.replace("+5.30","")
print(a)

现在,如何使用 python 中的日期时间将此字符串转换为日期? 我试过了:

c=datetime.datetime("%H:%M %p on %d/%m/%Y")
Print(c)

但这没有用,它显示错误。 怎么办?

根据您的代码,您可以使用 strptime 方法将字符串转换为日期时间对象。

import datetime 
b = "07:37 am GMT+5.30 on 10/11/2011"
a = b.replace("+5.30","")
a = datetime.datetime.strptime(a, "%I:%M %p GMT on %d/%m/%Y")
print(a)