Splitting textfile into section with special delimiter line - python

前端 未结 3 1759
说谎
说谎 2020-12-19 14:58

I have an input file as such:

This is a text block start
This is the end

And this is another
with more than one line
and another line.

The

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-19 15:32

    How about something like this?

    from itertools import groupby
    
    def per_section(s, delimiters=()):
        def key(s):
            return not s or s.isspace() or any(s.startswith(x) for x in delimiters)
        for k, g in groupby(s.splitlines(), key=key):
            if not k:
                yield list(g)
    
    
    if __name__ == '__main__':
        print list(per_section('''This is a text block start
    This is the end
    
    And this is another
    with more than one line
    and another line.'''))
    
        print list(per_section('''# Some comments, maybe the title of the following section
    This is a text block start
    This is the end
    # Some other comments and also the title
    And this is another
    with more than one line
    and another line.''', ('#')))
    
    print list(per_section('''!! Some comments, maybe the title of the following section
    This is a text block start
    This is the end
    $$ Some other comments and also the title
    And this is another
    with more than one line
    and another line.''', ('!', '$')))    
    

    Output:

    [['This is a text block start', 'This is the end'], ['And this is another', 'with more than one line', 'and another line.']]
    [['This is a text block start', 'This is the end'], ['And this is another', 'with more than one line', 'and another line.']]
    [['This is a text block start', 'This is the end'], ['And this is another', 'with more than one line', 'and another line.']]
    

提交回复
热议问题