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?

有帮助吗?

解决方案 2

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.

其他提示

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'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top