In Python, how do I split a string and keep the separators?

前端 未结 13 1228
[愿得一人]
[愿得一人] 2020-11-22 03:26

Here\'s the simplest way to explain this. Here\'s what I\'m using:

re.split(\'\\W\', \'foo/bar spam\\neggs\')
-> [\'foo\', \'bar\', \'spam\', \'eggs\']
         


        
13条回答
  •  离开以前
    2020-11-22 03:47

    I found this generator based approach more satisfying:

    def split_keep(string, sep):
        """Usage:
        >>> list(split_keep("a.b.c.d", "."))
        ['a.', 'b.', 'c.', 'd']
        """
        start = 0
        while True:
            end = string.find(sep, start) + 1
            if end == 0:
                break
            yield string[start:end]
            start = end
        yield string[start:]
    

    It avoids the need to figure out the correct regex, while in theory should be fairly cheap. It doesn't create new string objects and, delegates most of the iteration work to the efficient find method.

    ... and in Python 3.8 it can be as short as:

    def split_keep(string, sep):
        start = 0
        while (end := string.find(sep, start) + 1) > 0:
            yield string[start:end]
            start = end
        yield string[start:]
    

提交回复
热议问题