Python 中将带空格的字符串转换为日期时间的最有效方法 MySql
Most Efficient Way in Python to Convert String with Whitespaces to DateTime for MySql
我有以下字符串格式的数据 "dd Mmm YYYY, HH:mm"(例如“2008 年 8 月 7 日,16:25”)
将 Python 中的此格式转换为 MySQL 的日期时间字符串格式 "YYYY-MM-DD HH:MM:SS" 的最有效方法是什么?
你的意思是在 Python 中隐蔽吗?
>>> from datetime import datetime
>>> t = datetime.strptime('07 Aug 2008, 16:25', '%d %b %Y, %H:%M')
>>> t.strftime('%Y-%m-%d %H:%M:%S')
'2008-08-07 16:25:00'
>>>
查看 the document 了解更多详情。
import datetime
dt = datetime.datetime.strptime(my_date_str, '%d %b %Y, %H:%M')
print dt.isoformat(' ')
应该这样做,虽然
a) 当我尝试时,我在 strptime 中得到了一些关于 day_abbr 的 AttributeError。似乎是我系统上的某种本地化问题。试试你的?
b) isoformat 还为您提供亚秒级,您可能不想要:/
无论如何,你想在日期时间库中研究的东西是strptime/strftime and isoformat(<- ISO 8601,最佳日期+时间格式evar;如果可能,尽量在任何地方使用它)。
我有以下字符串格式的数据 "dd Mmm YYYY, HH:mm"(例如“2008 年 8 月 7 日,16:25”)
将 Python 中的此格式转换为 MySQL 的日期时间字符串格式 "YYYY-MM-DD HH:MM:SS" 的最有效方法是什么?
你的意思是在 Python 中隐蔽吗?
>>> from datetime import datetime
>>> t = datetime.strptime('07 Aug 2008, 16:25', '%d %b %Y, %H:%M')
>>> t.strftime('%Y-%m-%d %H:%M:%S')
'2008-08-07 16:25:00'
>>>
查看 the document 了解更多详情。
import datetime
dt = datetime.datetime.strptime(my_date_str, '%d %b %Y, %H:%M')
print dt.isoformat(' ')
应该这样做,虽然
a) 当我尝试时,我在 strptime 中得到了一些关于 day_abbr 的 AttributeError。似乎是我系统上的某种本地化问题。试试你的?
b) isoformat 还为您提供亚秒级,您可能不想要:/
无论如何,你想在日期时间库中研究的东西是strptime/strftime and isoformat(<- ISO 8601,最佳日期+时间格式evar;如果可能,尽量在任何地方使用它)。