如何删除下一个特定句子?

时间:2015-05-10 09:33:52

标签: python parsing

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

以下是一个例子:

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

如果我想删除所有妈妈的句子和下面的语法句子,我想要保留孩子的语法句子,我可以使用哪些Python脚本?

4 个答案:

答案 0 :(得分:1)

它从文件中懒惰地成对读取,并跳过在特定条件下产生的值。下面的代码将过滤后的行转储到另一个文件。

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)

sentences.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

filtered.txt内容:

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

答案 1 :(得分:0)

执行以下操作:

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

只需将上面的内容翻译成python代码,就可以了。

答案 2 :(得分:0)

function SomeClass () {}
SomeClass.prototype.someVar = null;
SomeClass.prototype.someFunc = function () {}

答案 3 :(得分:0)

您可以使用namedTemporaryFile和shutil.move替换原始文件:

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")