如何从多个文件夹和文件中读取特定段落

2024-04-26 05:27:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个列表,其中包含我要打开的目录和文件名,从中读取段落并将该段落保存到列表中。你知道吗

问题是我不知道如何从文件中“过滤”出段落并插入到我的列表中。你知道吗

我的代码到目前为止。你知道吗

rr = []
file_list = [f for f in iglob('**/README.md', recursive=True) if os.path.isfile(f)]
for f in file_list:
  with open(f,'rt') as fl:
    lines = fl.read()
    rr.append(lines)
  print(rr)

我试图读取的文件的格式。段落开始和新段落之间的文本就是我要找的

There is text above this paragraph
## Required reading
    * line
    * line
    * line
     /n
### Supplementary reading
There is text bellow this paragraph

当我运行代码时,我从文件中得到了预期的所有行。你知道吗


Tags: 代码textin列表forislinerr
2条回答

解决了我的问题与字符串切片。你知道吗

基本上,我只是扫描每一行的开始字符串和结束字符串,并作出行了出来。然后将这些行附加到列表并写入文件。你知道吗

for f in file_list:
        with open(f, 'rt') as fl:
            lines = fl.read()
            lines = lines[lines.find('## Required reading'):lines.find('## Supplementary reading')]
            lines = lines[lines.find('## Required reading'):lines.find('### Supplementary reading')]
            lines = lines[lines.find('## Required reading'):lines.find('## Required reading paragraph')]
            rr.append(lines)

但是在我的列表和文件中仍然有“##必读”,所以我运行了第二个读/写方法。你知道吗

def removeHashTag():
    global line
    f = open("required_reading.md", "r")
    lines = f.readlines()
    f.close()
    f = open("required_reading.md", "w")
    for line in lines:
        if line != "## Required reading" + "\n":
            f.write(line)
    f.close()
removeHashTag()

您需要了解导入的文本的结构。段落是如何分开的?它看起来像“\n\n”吗?您能在“\n\n”上拆分文本文件并返回所需段落的索引吗?你知道吗

text = 'paragraph one text\n\nparagraph two text\n\nparagraph three text'.split('\n\n')[1]
print(text)
>>> 'paragraph two text'

另一个选项,如其他人所提到的,是正则表达式,也就是RegEx,您可以使用

import re

正则表达式用于在文本中查找模式。你知道吗

转到https://pythex.org/并抓取其中一个文档的样本,尝试找到与您要找到的段落匹配的模式。你知道吗

在这里了解有关RegEx的更多信息 https://regexone.com/references/python

相关问题 更多 >

    热门问题