I need to come up with a function which will take a single string and it will do the following :
2014-05-10T12:30:00
).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?
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.
The strftime() method returns a string representing date and time using date, time or datetime object.
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'
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.
>>>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With