检查字符串是否有日期,任何格式 [英] Check if string has date, any format

查看:150
本文介绍了检查字符串是否有日期,任何格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查字符串是否可以解析为日期?




  • 1990年1月19日

  • 1990年1月19日

  • 1990年1月19日

  • 01/19/1990

  • 01/19/90

  • 1990

  • 1990年1月

  • 1900年1月



这些都是有效的日期。如果有任何疑问,项目#3和最后一个项目之间的空间之间缺乏空间,那么可以通过在字母/字符和数字之间自动插入一个空格来容易地进行修正。



但是首先,基本原理:



我试着把它放在一个 if语句

 如果datetime.strptime(item,'%Y')或datetime.strptime(item,'%b%d %y')或datetime.strptime(item,'%b%d%Y')或datetime.strptime(item,'%B%d%y')或datetime.strptime(item,'%B%d%Y '):

但是这是一个try-exception块,并且不断返回如下: / p>

16343时间数据'JUNE1890'与格式'%Y'不匹配



除非它符合 if语句中的第一个条件。



要澄清,我实际上并不需要日期的价值 - 我只想知道是否是。理想的情况是这样的:

 如果项目是日期:
打印日期
否:
打印不是日期

有没有办法这样做? / p>

解决方案

查看 parse 函数https://pypi.python.org/pypi/python-dateutilrel =noreferrer> dateutils.parser 。它能够将几乎任何字符串解析为 datetime 对象。



如果你只想知道一个特定的字符串可以表示日期,您可以尝试以下功能:

  from dateutil.parser import parse 

def is_date(string):
try:
parse(string)
return True
除了ValueError:
return False

然后你有:

 >>> is_date(1990-12-1)
True
>>> is_date(xyznotadate)
False

注意事项: parse 可能会将某些字符串识别为不想作为日期处理的日期,例如23,4将被解析为 datetime.datetime(2023,4,10,0,0)。您可能需要额外的检查,如果你想抓住这些情况。


How do I check if a string can be parsed to a date?

  • Jan 19, 1990
  • January 19, 1990
  • Jan 19,1990
  • 01/19/1990
  • 01/19/90
  • 1990
  • Jan 1990
  • January1990

These are all valid dates. If there's any concern regarding the lack of space in between stuff in item #3 and the last item above, that can be easily remedied via automatically inserting a space in between letters/characters and numbers, if so needed.

But first, the basics:

I tried putting it in an if statement:

if datetime.strptime(item, '%Y') or datetime.strptime(item, '%b %d %y')  or datetime.strptime(item, '%b %d %Y')  or datetime.strptime(item, '%B %d %y')  or datetime.strptime(item, '%B %d %Y'):

But that's in a try-exception block, and, keeps returning something like this:

16343 time data 'JUNE1890' does not match format '%Y'

Unless, it met the first condition in the if statement.

To clarify, I don't actually need the value of the date - I just want to know if it is. Ideally, it would've been something like this:

if item is date:
    print date
else:
    print "Not a date"

Is there any way to do this?

解决方案

Have a look at the parse function in dateutils.parser. It's capable of parsing almost any string to a datetime object.

If you simply want to know whether a particular string could represent a date, you could try the following function:

from dateutil.parser import parse

def is_date(string):
    try: 
        parse(string)
        return True
    except ValueError:
        return False

Then you have:

>>> is_date("1990-12-1")
True
>>> is_date("xyznotadate")
False

One note of caution: parse might recognise some strings as dates which you don't want to treat as dates, e.g. "23, 4" will be parsed as datetime.datetime(2023, 4, 16, 0, 0). You might need additional checks if you want to catch these cases.

这篇关于检查字符串是否有日期,任何格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆