如何在python中将字符串转换为日期对象?
该字符串是:"24052010"(对应于格式:"%d%m%Y")
我不想要datetime.datetime对象,而是datetime.date.
Sil*_*ost 543
你可以strptime在datetimePython 的包中使用:
>>> import datetime
>>> datetime.datetime.strptime('24052010', "%d%m%Y").date()
datetime.date(2010, 5, 24)
Run Code Online (Sandbox Code Playgroud)
Thi*_*ter 80
import datetime
datetime.datetime.strptime('24052010', '%d%m%Y').date()
Run Code Online (Sandbox Code Playgroud)
Sch*_*uer 50
直接相关的问题:
怎么样?
datetime.datetime.strptime("2015-02-24T13:00:00-08:00", "%Y-%B-%dT%H:%M:%S-%H:%M").date()
Run Code Online (Sandbox Code Playgroud)
你得到:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/_strptime.py", line 308, in _strptime
format_regex = _TimeRE_cache.compile(format)
File "/usr/local/lib/python2.7/_strptime.py", line 265, in compile
return re_compile(self.pattern(format), IGNORECASE)
File "/usr/local/lib/python2.7/re.py", line 194, in compile
return _compile(pattern, flags)
File "/usr/local/lib/python2.7/re.py", line 251, in _compile
raise error, v # invalid expression
sre_constants.error: redefinition of group name 'H' as group 7; was group 4
Run Code Online (Sandbox Code Playgroud)
你尝试过:
<-24T13:00:00-08:00", "%Y-%B-%dT%HH:%MM:%SS-%HH:%MM").date()
Run Code Online (Sandbox Code Playgroud)
但你仍然得到上面的追溯.
回答:
>>> from dateutil.parser import parse
>>> from datetime import datetime
>>> parse("2015-02-24T13:00:00-08:00")
datetime.datetime(2015, 2, 24, 13, 0, tzinfo=tzoffset(None, -28800))
Run Code Online (Sandbox Code Playgroud)
use*_*754 21
如果你很懒,不想与字符串文字作斗争,你可以选择parser模块.
from dateutil import parser
dt = parser.parse("Jun 1 2005 1:33PM")
print(dt.year, dt.month, dt.day,dt.hour, dt.minute, dt.second)
>2005 6 1 13 33 0
Run Code Online (Sandbox Code Playgroud)
只是旁注,因为我们试图匹配any字符串表示,它比10倍慢strptime
你有一个这样的日期字符串,“24052010”,你想要日期对象,
from datetime import datetime
cus_date = datetime.strptime("24052010", "%d%m%Y").date()
Run Code Online (Sandbox Code Playgroud)
这个 cus_date 会给你日期对象。
您可以使用此方法从日期对象中检索日期字符串,
cus_date.strftime("%d%m%Y")
Run Code Online (Sandbox Code Playgroud)
还有另一个库,叫做arrow“非常好”,可以对 python 日期进行操作。
import arrow
import datetime
a = arrow.get('24052010', 'DMYYYY').date()
print(isinstance(a, datetime.date)) # True
Run Code Online (Sandbox Code Playgroud)
对于单值,该datetime.strptime方法是最快的
import arrow
from datetime import datetime
import pandas as pd
l = ['24052010']
%timeit _ = list(map(lambda x: datetime.strptime(x, '%d%m%Y').date(), l))
6.86 µs ± 56.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit _ = list(map(lambda x: x.date(), pd.to_datetime(l, format='%d%m%Y')))
305 µs ± 6.32 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit _ = list(map(lambda x: arrow.get(x, 'DMYYYY').date(), l))
46 µs ± 978 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
Run Code Online (Sandbox Code Playgroud)
对于值列表,pandaspd.to_datetime是最快的
l = ['24052010'] * 1000
%timeit _ = list(map(lambda x: datetime.strptime(x, '%d%m%Y').date(), l))
6.32 ms ± 89.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%timeit _ = list(map(lambda x: x.date(), pd.to_datetime(l, format='%d%m%Y')))
1.76 ms ± 27.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit _ = list(map(lambda x: arrow.get(x, 'DMYYYY').date(), l))
44.5 ms ± 522 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Run Code Online (Sandbox Code Playgroud)
对于 ISO8601 日期时间格式,这ciso8601是一个火箭
import ciso8601
l = ['2010-05-24'] * 1000
%timeit _ = list(map(lambda x: ciso8601.parse_datetime(x).date(), l))
241 µs ± 3.24 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Run Code Online (Sandbox Code Playgroud)