[英]How do I strtotime in python?
I'm scraping aa page that includes among other things, date information. 我正在抓一个包含日期信息等内容的页面。 So I have a variable named
warrant_issued
that contains u'11/5/2003'
-- I want to store this as a machine readable date. 所以我有一个名为
warrant_issued
的变量,其中包含u'11/5/2003'
- 我想将其存储为机器可读日期。 PHP has a handy strtotime
function that works fabulously. PHP有一个方便的
strtotime
功能,非常有效。 I was hoping that datetime 's strptime would help me but it doesn't seem to be available in my version of datetime
-- here is everything in my tab complete on datetime.
我希望datetime的strptime可以帮助我,但它似乎在我的
datetime
版本中没有 - 这是我的标签中的所有内容都在datetime.
完成datetime.
In [231]: datetime.
datetime.MAXYEAR datetime.__hash__ datetime.__sizeof__
datetime.MINYEAR datetime.__init__ datetime.__str__
datetime.__class__ datetime.__name__ datetime.__subclasshook__
datetime.__delattr__ datetime.__new__ datetime.date
datetime.__dict__ datetime.__package__ datetime.datetime
datetime.__doc__ datetime.__reduce__ datetime.datetime_CAPI
datetime.__file__ datetime.__reduce_ex__ datetime.time
datetime.__format__ datetime.__repr__ datetime.timedelta
datetime.__getattribute__ datetime.__setattr__ datetime.tzinfo
I'm using iPython 2.7.2+ 我正在使用iPython 2.7.2+
Am I barking up the wrong tree here? 我在这里吠叫错了吗? What's the best way to turn
u'11/5/2003'
into a date? 将
u'11/5/2003'
变成约会的最佳方法是什么?
strptime()
is definitely the right approach, it's just a class method for the datetime
class (confusingly part of the datetime
module). strptime()
绝对是正确的方法,它只是datetime
类的一个类方法(令人困惑的是datetime
模块的一部分)。
That is, datetime.datetime.strptime()
is what you're looking for (and not datetime.strptime()
. 也就是说,
datetime.datetime.strptime()
就是你要找的东西(而不是datetime.strptime()
。
Try this: 尝试这个:
For use with the datetime module
, documentation here 与
datetime module
, 此处提供文档
>>>import datetime
>>>a = u'11/5/2003'
>>>time1 = datetime.datetime.strptime(a, "%m/%d/%Y")
>>>print time1
datetime.datetime(2003, 11, 5, 0, 0)
In ipython
: 在
ipython
:
In [1]: import datetime
In [2]: a = u'11/5/2003'
In [3]: time1 = datetime.datetime.strptime(a, "%m/%d/%Y")
In [4]: print time1
2003-11-05 00:00:00
Use with the time module
, documentation here 使用
time module
,文档在这里
>>>import time
>>>a = u'11/5/2003'
>>>time1 = time.strptime(a, "%m/%d/%Y")
>>>print time1
time.struct_time(tm_year=2003, tm_mon=11, tm_mday=5, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=309, tm_isdst=-1)
Thanks to the comment reply from @David Cain: 感谢@David Cain的评论回复:
The dateutil library can parse datetime strings (inferring their format).
dateutil库可以解析日期时间字符串(推断其格式)。 However, note that "11/5/2003" is not an unambiguous format (MM/DD or DD/MM differs by locale), so dateutil should be used with caution in this case.
但请注意,“11/5/2003”不是一种明确的格式(MM / DD或DD / MM因区域设置而异),因此在这种情况下应谨慎使用dateutil。 – David Cain
- 大卫凯恩
So an alternative good practice is to use the dateutil library: 所以另一种好的做法是使用dateutil库:
>>> from dateutil.parser import parse
>>> dt = parse('2016/12/05 05:18 pm')
>>> dt
datetime.datetime(2016, 12, 5, 17, 18)
>>> dt.timestamp()
1480929480.0
>>> parse('16/11/12')
>>> datetime.datetime(2012, 11, 16, 0, 0)
strtotime
you use to work with in PHP may be most similar to dateparser
. 您在PHP中使用的
strtotime
可能与dateparser
最相似。
Note if you don't have the dateparser
library. 请注意,如果您没有
dateparser
库。
Install it with 安装它
pip3 install dateparser
If you are using conda 如果你正在使用conda
conda install dateparser
This library understands over 200 language locales plus numerous formats in a language agnostic fashion. 该库可以理解超过200种语言区域以及与语言无关的多种格式。
Parsing generic terms also works: 解析通用术语也有效:
import dateparser
print (dateparser.parse('yesterday')) #2019-05-19 08:08:14.934992
And also supports non-Gregorian calendar systems. 并且还支持非公历日历系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.