Python “split” on empty new line

前端 未结 3 1377
野的像风
野的像风 2020-12-16 03:54

Trying to use a python split on a \"empty\" newline but not any other new lines. I tried a few other example I found but none of them seem to work.

Data example:

3条回答
  •  Happy的楠姐
    2020-12-16 04:13

    This works in the case where multiple blank lines should be treated as one.

    import re
    
    def split_on_empty_lines(s):
    
        # greedily match 2 or more new-lines
        blank_line_regex = r"(?:\r?\n){2,}"
    
        return re.split(blank_line_regex, s.strip())
    

    The regex is a bit odd.

    1. Firstly, the greedy matching means that many blank lines count as a single match, i.e. 6 blank lines makes one split, not three splits.
    2. Secondly, the pattern doesn't just match \n but either \r\n (for Windows) or \n (for Linux/Mac).
    3. Thirdly, the group (denoted by parentheses) needs to have ?: inside the
      opening parenthesis to make it a "non-capturing" group, which changes the behaviour of re.split.

    For example:

    s = """
    
    hello
    world
    
    this is
    
    
    
    
    
    
    
    a test
    
    """
    
    split_on_empty_lines(s)
    

    returns

    ['hello\nworld', 'this is', 'a test']
    

提交回复
热议问题