Search code examples
pythonstringtimestampstrip

strip timestamp/syntax-sequence from python string


I'm working on a python script which retrieves values from an sdict as a string value. Many of the returned values contain a timestamp depending on the sdict, which is a tad annoying.

Eg. <2006-12-20 00:10:24 Cattle is a tree>

I cannot for the life of me, figure out how to remove the 2006-12-20 00:10:24 from the returned value. Has anyone got any ideas? I was thinking I could strip anything between - characters. However, the returned values often contain - throughout.


Solution

  • This is a good candidate for using regular expressions.

    >>> import re
    >>> s = '<2006-12-20 00:10:24 Cattle is a tree>'
    >>> re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ', '', s)
    '<Cattle is a tree>'