How to get part of string and pass it to other function in python?

前端 未结 4 427
一整个雨季
一整个雨季 2021-01-28 06:58

My task is to verify whether string is valid date and time or not.

\"2013-01-01W23:01:01\"

The date and the time are separated with

4条回答
  •  借酒劲吻你
    2021-01-28 07:37

    Use str.partition() to get the first part, fast:

    value.partition('W')[0]
    

    You could use str.split() as well, for Python versions < 2.5. Limit the split to just the first W:

    value.split('W', 1)[0]
    

    Either technique will always yield a result, even if there is no 'W' character in value.

    Demo:

    >>> value = "2013-01-01W23:01:01"
    >>> value.partition('W')[0]
    '2013-01-01'
    >>> value.split('W', 1)[0]
    '2013-01-01'
    

提交回复
热议问题