Splitting textfile into section with special delimiter line - python

谁说我不能喝 提交于 2019-11-29 11:14:18

How about pass a predicate?

def per_section(it, is_delimiter=lambda x: x.isspace()):
    ret = []
    for line in it:
        if is_delimiter(line):
            if ret:
                yield ret  # OR  ''.join(ret)
                ret = []
        else:
            ret.append(line.rstrip())  # OR  ret.append(line)
    if ret:
        yield ret

Usage:

with open('/path/to/file.txt') as f:
    sections = list(per_section(f))  # default delimiter

with open('/path/to/file.txt.txt') as f:
    sections = list(per_section(f, lambda line: line.startswith('#'))) # comment

Simply do this:

with open('yorfileaname.txt') as f: #open desired file
    data = f.read() #read the whole file and save to variable data
    print(*(data.split('=========='))) #now split data when "=.." and print it 
    #usually it would ouput a list but if you use * it will print as string

Output:

content content
more content
content conclusion

content again
more of it
content conclusion

content
content
contend done

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.']]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!