如何提取文本文件中不同的数据子集并将每个子集传递到另一个文本文件中?

How to Extract different subset of data in text file and pass each subset into another text file?

我的文本文件很少,我需要对副标题数据和副标题数据的内容进行子集化,然后传递给另一个文件。

文本文件看起来像这样

Notes 

1. content

2. here also there will be some content till n lines

rule Note 

1. n line content (a) for every section

Add Notes

(a) some content

other Note

1. the rest of file
***Code***
    with open(file,encoding='utf8') as in_file: 
        s = in_file.read() 

        for i, char in enumerate(s): 
            if s[i:i+5] == 'Notes': 
                break      

        for j in range(i,0,-1): 
            if s[j] == '\n': 
                break
        rest_of_file = s[j+1:]

以上代码从Notes的文本文件中提取数据。 所以我的预期输出在第一次迭代中看起来像这样,需要传递给另一个文件

Notes 

1. content

2. here also there will be some content till n lines

第二次迭代

rule Note 

1. n line content (a) for every section

第三次迭代

Add Notes

(a) some content

最终迭代

other Note

1. the rest of file

注意: 这是一个所有子标题都带有样式的文件,但所有文本文件可能都不相同。有些文件可能会遗漏注释,有些可能会遗漏规则注释和添加注释,有些文件可能会直接有其他注释,这可能会发生

我在这里找到的唯一常见模式是注释

任何方法都可以,任何人都可以帮助解决这个问题... 也准备和美汤一起工作了

这个方法是

  1. 已将所有内容传递到列表中
  2. 如果注释出现在项目中,则将项目的索引获取到列表中
  3. 根据索引列表将其分为不同的部分

示例代码在这里: