Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从文本文件中获取两个特定单词之间的所有单词,并使用python将其写入新的文本文件中_Python - Fatal编程技术网

如何从文本文件中获取两个特定单词之间的所有单词,并使用python将其写入新的文本文件中

如何从文本文件中获取两个特定单词之间的所有单词,并使用python将其写入新的文本文件中,python,Python,假设我有一个包含 Section 1 What: random1 When: random2 Why: random3 Where: random4 How: random5 Section 2 What: dog1 When: dog2 Why: dog3 Where: dog4 How: dog5 Section 3 What: me1 When: me2 Why: me3 Where: me4 How: me5 我想创建一个函数来获取文本文件,查找两个单词,复制中间的所有内容,并不

假设我有一个包含

Section 1
What: random1 
When: random2
Why:  random3 
Where: random4
How: random5
Section 2
What: dog1
When: dog2
Why: dog3
Where: dog4
How: dog5
Section 3
What: me1
When: me2
Why: me3
Where: me4
How: me5
我想创建一个函数来获取文本文件,查找两个单词,复制中间的所有内容,并不断收集数据并将其放入新的文本文件中

例如:def my_functiondocument,start,end:在交互窗口中,我将放置my_functiontesting.txt、何时、为什么以及它应该创建一个包含以下数据的新文本文件:

when: random2
when: dog2
when: me2
因此,该函数获取这两个单词之间的所有数据,并且这两个单词多次出现,因此它必须继续查看文件

另一个线程中的用户发布了一个可能对我有帮助的解决方案,但我不确定如何将其放入函数中,并且我不理解使用的代码

这是一个解决方案,作者:falsetru

import itertools

with open('data.txt', 'r') as f, open('result.txt', 'w') as fout:
   while True:
      it = itertools.dropwhile(lambda line: line.strip() != 'Start', f)
      if next(it, None) is None: break
      fout.writelines(itertools.takewhile(lambda line: line.strip() != 'End', it))
这将产生以下输出:

>>> fn('testing.txt', 'when', 'why')
When: random2
When: dog2
When: me2
它的工作原理是逐行遍历文件,并在每行以start开头时设置一个标志True,在每行以end开头时设置一个标志False。当标志为True时,将打印该行


由于文章中的示例大小写混合,我使用了lower方法使测试不区分大小写。

这将完成您描述的内容。我添加了一个dest_路径输入来指定输出文件

def my_function(source_path, dest_path, start_text, stop_text):
    # pre-format start and end to save time in loop (for case insensitive match)
    lower_start = start_text.lower()
    lower_stop = stop_text.lower()
    # safely open input and output files
    with open(source_path, 'r') as source, open(dest_path, 'w') as dest:
        # this variable controls if we're writing to the destination file or not
        writing = False
        # go through each line of the source file
        for line in source:
            # test if it's a starting or ending line
            if line.lower().startswith(lower_start): writing = True
            elif line.lower().startswith(lower_stop): writing = False
            # write line to destination file if needed
            if writing: dest.write(line)

请注意,with块完成后,文件将自动关闭。

函数是否返回标题谁、什么、何时、何地、为什么或计算标题?我这样问是因为在你的例子中,输出的大小写与输入的大小写不同。函数返回的是开始,即示例中的单词,以及它后面的所有单词,直到它到达结尾,这就是为什么
def my_function(source_path, dest_path, start_text, stop_text):
    # pre-format start and end to save time in loop (for case insensitive match)
    lower_start = start_text.lower()
    lower_stop = stop_text.lower()
    # safely open input and output files
    with open(source_path, 'r') as source, open(dest_path, 'w') as dest:
        # this variable controls if we're writing to the destination file or not
        writing = False
        # go through each line of the source file
        for line in source:
            # test if it's a starting or ending line
            if line.lower().startswith(lower_start): writing = True
            elif line.lower().startswith(lower_stop): writing = False
            # write line to destination file if needed
            if writing: dest.write(line)