从字符串 datetime.strptime(date_string,format)获取日期时间的函数需要一个字符串格式作为第二个参数。有没有办法从字符串构建datetime而不知道确切的格式,并且Python最好的猜测?
The function to get a datetime from a string, datetime.strptime(date_string, format) requires a string format as the second argument. Is there a way to build a datetime from a string without without knowing the exact format, and having Python best-guess it?
推荐答案使用 dateutil 库。
我已经使用dateutil作为处理时区的不可或缺lib (请参阅将UTC datetime字符串转换为本地日期时间和如何将本地时间转换为Python的UTC?)
I was already using dateutil as an indispensable lib for handling timezones (See Convert UTC datetime string to local datetime and How do I convert local time to UTC in Python?)
我刚刚意识到它有日期解析支持:
And I've just realized it has date parsing support:
import dateutil.parser yourdate = dateutil.parser.parse(datestring)(另请参见如何将ISO 8601 datetime字符串转换为Python datetime对象?)
更多推荐
datetime从Python中的字符串,最好的猜测字符串格式
发布评论