2016-01-03 3 views
1

У меня есть текстовый файл, содержащий книгу, и мне нужен последний параграф каждого раздела.найти последний параграф каждого раздела из книги

Я могу найти первый параграф каждого раздела. Я использовал regex .. Я пытался изменить свой код, но я не мог этого добиться. Можете ли вы дать мне представление о достижении последнего параграфа каждого раздела?

Вот мой код, который находит первые пункты

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. 
+2

Я не знаю python, но может быть, это регулярное выражение может помочь вам \ n ([^ \ n] *) \ n (? = \ N [IVXLCDM] * \ n | $) ' –

ответ

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. 
+0

Он отлично работает! благодаря.. –

Смежные вопросы