我有这样的日期字符串:
Saturday, 30 Nov, 2013
所以它就像 Day_Name, Day, Month_Name_3_Letters, Year
。
我想知道使用 python 将其转换为日期时间格式的最佳方法是什么?
我是这样使用的:
datetime.strptime((row[7].split(',')[1] + row[7].split(',')[2]).replace(' ',''), "%d%b%Y").strftime("%Y-%m-%d")
最佳答案
使用strptime
:
import datetime as dt
s = 'Saturday, 30 Nov, 2013'
d = dt.datetime.strptime(s,'%A, %d %b, %Y')
结果:
>>> d
datetime.datetime(2013, 11, 30, 0, 0)
正如您将从引用中看到的那样:
%A Weekday as locale’s full name. %d Day of the month as a zero-padded decimal number. %b Month as locale’s abbreviated name. %Y Year with century as a decimal number.
关于Python 将不寻常的日期字符串转换为日期时间格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43786415/