【问题标题】:Invalid datetime error无效的日期时间错误
【发布时间】:2016-12-06 16:10:46
【问题描述】:

我正在使用 datefinder 模块在文本中查找日期参考。但是,函数调用返回的生成器可能会返回无效日期。如何使用 try-except 块解决这个问题?

import datefinder
file = open('abc.txt', 'r')    
for text in file:
     dates = datefinder.find_dates(text)

     for one_date in dates:        #Getting an error at this point if date is not in the range
         if valid_date(one_date):
             #some code here

【问题讨论】:

  • 您遇到的错误是什么?返回无效日期时会发生什么?
  • 获取值错误。解析器可能会返回无效日期,例如 >2000 作为一年

标签: python python-datetime try-except


【解决方案1】:
import datefinder
file = open('abc.txt', 'r')    
for text in file:
       dates = datefinder.find_dates(text)
       while True:
                    try:
                        one, flag = self.valid_date((str1.__iter__()).next())
                        if flag:
                            *some code here*
                    except ValueError:
                        continue
                    except StopIteration:
                        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多