How can I split by 1 or more occurrences of a delimiter in Python?

前端 未结 6 1036
旧时难觅i
旧时难觅i 2020-12-05 09:56

I have a formatted string from a log file, which looks like:

>>> a=\"test                            result\"

That is, the test an

6条回答
  •  时光取名叫无心
    2020-12-05 10:25

    Just this should work:

    a.split()
    

    Example:

    >>> 'a      b'.split(' ')
    ['a', '', '', '', '', '', 'b']
    >>> 'a      b'.split()
    ['a', 'b']
    

    From the documentation:

    If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

提交回复
热议问题