Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string contains date or timestamp in python [closed]

I need to come up with a function which will take a single string and it will do the following :

  1. check if it is a timestamp in UTC format (e.g. if it is of the form 2014-05-10T12:30:00).
  2. If it is in the format described above, replace 'T' with space and return the string.
  3. If it is not of timestamp, simply return the string.

What is the best way to accomplish this in python? I thought I could use datetime module. But can this be done using re module?

like image 245
Santanu C Avatar asked May 14 '14 01:05

Santanu C


People also ask

How do you check if a string contains a date in Python?

Method #1 : Using strptime() In this, the function, strptime usually used for conversion of string date to datetime object, is used as when it doesn't match the format or date, raises the ValueError, and hence can be used to compute for validity.

How do you timestamp A string in Python?

The strftime() method returns a string representing date and time using date, time or datetime object.


2 Answers

While zmo's answer is correct, I see many people, especially experienced sys-admins, who are excellent with regex opt, to often, to crafting their own regular expression. Regular expressions are hard to maintain and to read, and Python's own STL offers some great tried and tested way to do it without the need to re-invent the correct regular expression. Here is my 2 cent, Pythonic solution:

In[87]: import time

In[88]: correct = "2014-05-10T12:30:00"
In[89]: wrong = "some string" # will raise ValueError

In[90]: try:
           time.strptime(correct, "%Y-%m-%dT%H:%M:%S")
           correct = correct.replace('T',' ')
        except ValueError:
           pass
        .... 

In [91]: correct
Out[91]: '2014-05-10 12:30:00'

In [93]: wrong = "foo bar baz"

In [94]: try:                 
            time.strptime(wrong, "%Y-%m-%dT%H:%M:%S")
            correct = correct.replace('T',' ')
         except ValueError:
            pass
        .... 

In [95]: wrong
Out[95]: 'foo bar baz'
like image 91
oz123 Avatar answered Nov 07 '22 09:11

oz123


you can match using a regex:

>>> s1 = "1) check if it is a timestamp in UTC format (e.g. if it is of the form '2014-05-10T12:30:00')."
>>> s2 = "3) If it is not of timestamp, simply return the string."
>>> re.compile('\d\d\d\d-\d\d-\d\d\(T\)\d\d:\d\d:\d\d')
<_sre.SRE_Pattern object at 0x7f9781558470>
>>> s = re.sub(r'(.*\d\d\d\d-\d\d-\d\d)T(\d\d:\d\d:\d\d.*)',r'\1 \2',s1)
>>> print(s)
1) check if it is a timestamp in UTC format (e.g. if it is of the form '2014-05-10 12:30:00').
>>> s = re.sub(r'(.*\d\d\d\d-\d\d-\d\d)T(\d\d:\d\d:\d\d.*)',r'\1 \2',s2)
>>> print(s)
3) If it is not of timestamp, simply return the string.
>>> 

Regular expression visualization

Play with it

The trick here, is to catch groups left and right of the T character, and paste them again around a space. As a bonus, if there's no match, there's no substitution.

like image 3
zmo Avatar answered Nov 07 '22 09:11

zmo