I have a book in a text file and I need to print first paragraph of each section. I thought that if I found a text between \n\n and \n I can find my answer. Here is my codes and it didn't work. Can you tell me that where am I wrong ?
lines = [line.rstrip('\n') for line in open('G:\\aa.txt')]
check = -1
first = 0
last = 0
for i in range(len(lines)):
if lines[i] == "":
if lines[i+1]=="":
check = 1
first = i +2
if i+2< len(lines):
if lines[i+2] == "" and check == 1:
last = i+2
while (first < last):
print(lines[first])
first = first + 1
Also I found a code in stackoverflow I tried it too but it just printed an empty array.
f = open("G:\\aa.txt").readlines()
flag=False
for line in f:
if line.startswith('\n\n'):
flag=False
if flag:
print(line)
elif line.strip().endswith('\n'):
flag=True
I shared a sample section of this book in belown.
I
THE LAY OF THE LAND
There is a vast field of fascinating human interest, lying only just outside our doors, which as yet has been but little explored. It is the Field of Animal Intelligence.
Of all the kinds of interest attaching to the study of the world's wild animals, there are none that surpass the study of their minds, their morals, and the acts that they perform as the results of their mental processes.
II
WILD ANIMAL TEMPERAMENT & INDIVIDUALITY
What I am trying to do here is, find the uppercase lines, and put them all in an array. Then, using the index method, I will find the first and last paragraphs of each section by comparing the indexes of these elements of this array I created.
Output should be like this :
There is a vast field of fascinating human interest, lying only just outside our doors, which as yet has been but little explored. It is the Field of Animal Intelligence.
What I am trying to do here is, find the uppercase lines, and put them all in an array. Then, using the index method, I will find the first and last paragraphs of each section by comparing the indexes of these elements of this array I created.
If you want to group the sections you can use
itertools.groupby
using empty lines as the delimiters:With some more itertools foo we can get the sections using the uppercase title as the delimiter:
Which would give you:
The start of each section has an all uppercase title so once we hit that we know there are two empty lines then the first paragraph and the pattern repeats.
To break it into using loops:
For your text:
The dollar signs are the new lines, the output is:
Go over the code you have found, line by line.
It seems it never sets the flag variable as true.
And if you can share some samples from your book it will be more helpful for everyone.
There's always regex....
This should work, as long as there are no paragraphs with all caps:
If you want to get the last paragraph too, you can keep track of the line last seen that contained lowercase chars and then as soon as you find an all uppercase line (I, II, etc), indicating a new section, then you print the most recent line, since that would be the last paragraph in the previous section.
TXR solution
Code in
firstpar.txr
:Basically we are searching the input for a pattern match for the three-element multi-line pattern which binds the
num
,title
andfirstpar
variables. Now this pattern, as such, can match in wrong places, so add some constraining heuristics with arequire
assertion. The section number is required to be a short line, and a title line must contain some upper case letters, and no lower-case ones. This expression is written in TXR Lisp.If we get a match with this constraint then we output the string captured in the
firstpar
variable.