for循环后每行打印一个句子

时间:2018-10-18 16:01:56

标签: python

我有一个文本文件,我想1)遍历其每个句子,然后2)遍历其句子的每个单词以修改其中的一些内容,然后3)打印结果新版本的文本,每个句子一个句子线

这是我到目前为止尝试过的:

import my_text

for sentence in my_text.sents():
    for word in sentence:
        if word == "the":
            print("article", end= " ")
        else:
            print("non-article", end= " ")
     if word == sentence[-1]:
        print("\n")

此代码有效,我的文本被修改并每行打印一个句子。但是,每个句子之间都有一个空行,我想删除它。示例:

article non-article non-article non-article

non-article non-article non-article non-article non-article non-article

article non-article non-article non-article

这就是我想要的:

article non-article non-article non-article
non-article non-article non-article non-article non-article non-article
article non-article non-article non-article

我该怎么办?

2 个答案:

答案 0 :(得分:1)

问题出在print("\n")中,end的默认值是"\n",因此每次您print("\n")时它都会打印"\n\n"。只需使用print()

sentences = ["the cat and dog", "the mouse and cat", "the fox and else"]
sentences = [sentence for sentence in map(str.split, sentences)]

for sentence in sentences:
    for word in sentence:
        if word == "the":
            print("article", end=" ")
        else:
            print("non-article", end=" ")

        if word == sentence[-1]:
            print()

输出

article non-article non-article non-article 
article non-article non-article non-article 
article non-article non-article non-article 

答案 1 :(得分:0)

您可以这样做:

import my_text

for sentence in my_text.sents():
  end = " "
  for word in sentence:
    if word == sentence[-1]:
        end = "\n"
    if word == "the":
        print("article", end=end)
    else:
        print("non-article", end=end)