从书中找到每个部分的最后一段

时间:2016-01-03 23:21:42

标签: python regex paragraph

我有一个包含书籍的文本文件,我需要每个部分的最后一段。

我可以找到每个部分的第一段。我使用正则表达式..我试图改变我的代码,但我无法实现。您能否告诉我有关达到每个部分最后一段的内容?

这是我的代码,找到第一段

print("\nFIRST PARAGRAPH OF EACH SECTION\n")
    with open(fileName) as fi:
        data = fi.read()

    #line of Roman numeral characters
    #line without lower case characters ( titles )
    #find first paragraph

    paras = re.findall(r"""
                       [IVXLCDM]+\n\n   
                       [^a-z]+\n\n
                       ((?:[^\n][\n]?)+)
                       """, data, re.VERBOSE)
    print ("\n\n".join(paras))

给出了书的一部分。

XVIII

THE MORALS OF WILD ANIMALS


The ethics and morals of men and animals are thoroughly
comparative, and it is only by direct comparisons that they can be
analyzed and classified. It is quite possible that there are quite
a number of intelligent men and women who are not yet aware of the
fact that wild animals _have_ moral codes, and that on an
average they live up to them better than men do to theirs.

.............(here is the body part of section)

Only two wild animal species known to me,--wolves and crocodiles,
--devour their own kind; but many of the races of men have been
cannibals, and some are so today.


XIX


THE LAWS OF THE FLOCKS AND THE HERDS

I approach this subject with a
feeling of satisfaction; but I would not like to state the
number of hours that I have spent in watching the play of our
wild animals.

.............(here is the body part of section)

In playing, young chimps, orangs or monkeys seize each other and
wrestle, fall, and roll over and over, indefinitely. They make
great pretenses of biting each other, but it is all make-believe.
My favorite orang-utan pet in Borneo loved to play at biting me,
but whenever the pressure became too strong I would say chidingly,
"Ah! Ah!" and his jaws would instantly relax. He loved to butt me
in the chest with his head, make wry faces, and make funny noises
with his lips. I tried to teach him "cat's cradle" but it was too
much for him. His clumsy fingers could not manage it.

输出应该是这样的:

Only two wild animal species known to me,--wolves and crocodiles,
--devour their own kind; but many of the races of men have been
cannibals, and some are so today.

In playing, young chimps, orangs or monkeys seize each other and
wrestle, fall, and roll over and over, indefinitely. They make
great pretenses of biting each other, but it is all make-believe.
My favorite orang-utan pet in Borneo loved to play at biting me,
but whenever the pressure became too strong I would say chidingly,
"Ah! Ah!" and his jaws would instantly relax. He loved to butt me
in the chest with his head, make wry faces, and make funny noises
with his lips. I tried to teach him "cat's cradle" but it was too
much for him. His clumsy fingers could not manage it.

1 个答案:

答案 0 :(得分:0)

您可以在没有复杂正则表达式的情况下使用re.split()str.split()

with open(fileName) as fi:
    data = fi.read()

l = [i.strip() for i in re.split('[IVXLCDM]+\n\n', text) if i.strip()]
for i in l:
    print(i.split('\n\n')[-1])
    print('\n')

演示:

Only two wild animal species known to me,--wolves and crocodiles,
--devour their own kind; but many of the races of men have been
cannibals, and some are so today.


In playing, young chimps, orangs or monkeys seize each other and
wrestle, fall, and roll over and over, indefinitely. They make
great pretenses of biting each other, but it is all make-believe.
My favorite orang-utan pet in Borneo loved to play at biting me,
but whenever the pressure became too strong I would say chidingly,
"Ah! Ah!" and his jaws would instantly relax. He loved to butt me
in the chest with his head, make wry faces, and make funny noises
with his lips. I tried to teach him "cat's cradle" but it was too
much for him. His clumsy fingers could not manage it.