1

在 Ubuntu 12.04 上的 Python 2.7 上使用 Bear 的 parsedatetime 库。当传入一个包含字符但没有有效日期(或时间)的字符串时,parse() 函数有时会返回一个有效的日期/时间。

提交第 48 期后,看起来 parsedatetime 真的不想要一个没有日期/时间的字符串。

那么,关于预处理器确定字符串是否包含有效日期的布尔值的想法?

参考链接: https ://github.com/bear/parsedatetime/ https://code.google.com/p/parsedatetime/issues/detail?id=48

注意:在第 48 期中,我不恰当地调用了 parseDateText()。我已经确认 parse() 存在相同的操作。

非常感谢任何帮助。

编辑:示例代码。

Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import parsedatetime as pdt
>>> c = pdt.Constants()
>>> c.BirthdayEpoc = 80
>>> p = pdt.Calendar(c)
>>> print p.parse("Mary had a little lamb.")
((2014, 3, 1, 20, 53, 56, 6, 69, 1), 1)
>>> print p.parse("foo bar")
(time.struct_time(tm_year=2013, tm_mon=3, tm_mday=10, tm_hour=20, tm_min=54, tm_sec=6, tm_wday=6, tm_yday=69, tm_isdst=1), 0)
>>> print p.parse("March 12th, 2013")
((2013, 3, 12, 20, 54, 34, 6, 69, 1), 1)
>>>
4

1 回答 1

1

正如 JF 所提到的,正确调用使用的是 parse() 方法。parse 方法还具有返回包含两项的元组的值:一项是确定的日期/时间值,另一个是显示是否找到任何日期/时间值的标志...

以下是返回标志值的细分:

  • 0 = 根本不解析
  • 1 = 解析为日期
  • 2 = 解析为时间
  • 3 = 解析为日期时间

如果您从测试中得到的不是 0,那么您发现了一个错误:/

于 2013-03-11T01:15:15.530 回答