Python 如何在删除下一个句子的同时删除特定的句子?

Python 如何在删除下一个句子的同时删除特定的句子?,python,parsing,Python,Parsing,我正在处理文本文件中的成绩单,我需要去掉一些特定的行 以下是一个例子: 孩子:妈妈,我能吃冰淇淋吗? 语法:名词动词前动词艺术名词 妈妈:这是一些钱,去买冰淇淋吧 语法:adv动词前名词 孩子:但是我想要更多的钱,我想要大冰激凌 语法:连词前动词名词前动词艺术名词 如果我想删除所有妈妈的句子和下面的语法句子(我想保留孩子的语法句子),我可以使用什么Python脚本?它以两人一组的方式懒洋洋地读取文件,并在特定条件下跳过生成值。函数下面的代码将过滤后的行转储到另一个文件 def yield_fi

我正在处理文本文件中的成绩单,我需要去掉一些特定的行

以下是一个例子:

孩子:妈妈,我能吃冰淇淋吗? 语法:名词动词前动词艺术名词 妈妈:这是一些钱,去买冰淇淋吧 语法:adv动词前名词 孩子:但是我想要更多的钱,我想要大冰激凌 语法:连词前动词名词前动词艺术名词
如果我想删除所有妈妈的句子和下面的语法句子(我想保留孩子的语法句子),我可以使用什么Python脚本?

它以两人一组的方式懒洋洋地读取文件,并在特定条件下跳过生成值。函数下面的代码将过滤后的行转储到另一个文件

def yield_filtered_lines(filename):
    with open(filename) as f:
        while True:
            sentence = f.readline()
            grammar = f.readline()
            if not sentence or not grammar:
                break  # EOF
            if sentence.startswith("Mom:"):
                continue
            yield sentence
            yield grammar

with open('filtered.txt', 'w') as f:
    for l in yield_filtered_lines("sentences.txt"):
        f.write(l)
句子.txt
内容:

Child:    Mom   can   I    have  an  ice cream?
grammar:  noun  verb  pro  verb  art noun
Mom:      Here is   some  money, go    buy   that  ice cream
grammar:  adv  verb pro   noun   verb  verb  pro   noun
Child:    But  I   want more money, I   want the  big  ice cream
grammar:  conj pro verb adj  noun   pro verb art  adj  noun
Child:    Mom   can   I    have  an  ice cream?
grammar:  noun  verb  pro  verb  art noun
Child:    But  I   want more money, I   want the  big  ice cream
grammar:  conj pro verb adj  noun   pro verb art  adj  noun
filtered.txt
内容:

Child:    Mom   can   I    have  an  ice cream?
grammar:  noun  verb  pro  verb  art noun
Mom:      Here is   some  money, go    buy   that  ice cream
grammar:  adv  verb pro   noun   verb  verb  pro   noun
Child:    But  I   want more money, I   want the  big  ice cream
grammar:  conj pro verb adj  noun   pro verb art  adj  noun
Child:    Mom   can   I    have  an  ice cream?
grammar:  noun  verb  pro  verb  art noun
Child:    But  I   want more money, I   want the  big  ice cream
grammar:  conj pro verb adj  noun   pro verb art  adj  noun
请执行以下操作:

  • 使用for循环迭代文本文件
  • 对于文件中的每一行,测试其开头,例如line.startswith(“Child:”)
  • 如果为True,则将该行写入新文件(在启动for循环之前打开)

只需将上述内容翻译成python代码,您就可以使用namedTemporaryFile和shutil.move替换原始文件:

with open('input.txt') as infile, open('output.txt', 'w') as outfile:
    for quote in infile:
        grammar = next(infile)
        if quote.startswith('Child'):
            outfile.write(quote + grammar)
from tempfile import NamedTemporaryFile
from shutil import move

with open('in.txt') as f, NamedTemporaryFile(dir=".", delete=False) as out:
    for line in f:
        if line.startswith("Child"):
            out.writelines((line,next(f)))

move(out.name, "temp.txt")

请发布您想要的输出。对不起。。。在python中…我只想要孩子的句子和语法。这有意义吗?同样,在txt文件中,它位于不同的行上,所以一行是给孩子的,下一行是语法,下一行是妈妈等等…一次读两行,然后决定第一行的第一个单词。几乎。。。但我只希望输出显示孩子的句子和孩子句子后面的语法句子。我想要“妈妈”这句话和跟在“妈妈走了”后面的语法。。。所以我有一个输出文件,里面有孩子说的话,还有孩子的语法sentence@ChristinaLindeDideriksen修正了。我不知道为什么这被否决了,但如果OP连一行代码都不提供,我可以只给出一个解释而不是代码。这不能完成要求的任务,它只会写出以该短语开头的行,而不会写出下一行。