Разбиение текстового файла в раздел с помощью специальной разделительной строки - python

У меня есть входной файл как таковой:

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.']]

Я получил желаемый результат, сделав так:

def per_section(it):
    """ Read a file and yield sections using empty line as delimiter """
    section = []
    for line in it:
        if line.strip('\n'):
            section.append(line)
        else:
            yield ''.join(section)
            section = []
    # yield any remaining lines as a section too
    if section:
        yield ''.join(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.

Я должен сделать это:

def per_section(it):
    """ Read a file and yield sections using empty line as delimiter """
    section = []
    for line in it:
        if line[0] != "#":
            section.append(line)
        else:
            yield ''.join(section)
            section = []
    # yield any remaining lines as a section too
    if section:
        yield ''.join(section)

Если бы я позволилper_section() чтобы иметь параметр разделителя, я мог бы попробовать это:

def per_section(it, delimiter== '\n'):
    """ Read a file and yield sections using empty line as delimiter """
    section = []
    for line in it:
        if line.strip('\n') and delimiter == '\n':
            section.append(line)
        elif delimiter= '\#' and line[0] != "#":
            section.append(line)
        else:
            yield ''.join(section)
            section = []
    # yield any remaining lines as a section too
    if section:
        yield ''.join(section)

Но есть ли способ, чтобы я не кодировал все возможные разделители?

Ответы на вопрос(3)

Ваш ответ на вопрос