2

Imagine I have this text in a txt file:

bla bla bla
bla bla bla
Title Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore et dolore
magna aliqua. Ut enim ad minim veniam,
condition
bla bla bla
bla bla bla
Title Sed ut perspiciatis unde omnis iste natus error sit voluptatem
accusantium doloremque laudantium, totam rem aperiam,
eaque ipsa quae ab illo inventore veritatis
condition
bla bla bla

From the text with the structure above (hundred of lines), I want to extract the lines that start with 'title' until I find the line that starts with the word 'condition'. So the result would be something like this:

Title Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

Title Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis

I get to select the first like with this code, but I don't know how to add the next lines until I find the word 'condition'. Could you help me, please?

outF = open("myOutFile.txt", "w")
hand = open('doubt.txt', encoding="utf8")
for line in hand:
    line = line.rstrip()
    if re.search('^Title',line) :       
       outF.write(line); outF.write("\n")
       outF.write("\n")
outF.close()```
nekovolta
  • 496
  • 2
  • 14

1 Answers1

2

In case you want all titles until the first condition line appears, you need to break the loop:

for line in hand:
    line = line.rstrip()
    if line.startswith("Title"):       
       outF.writelines([line])
    if line.startswith("condition"):
         break

outF.close()

In case you want to write all lines after a title till the next condition appears:

write = False
writelines = []

for line in hand:
    line = line.rstrip()
    
    if line.startswith("condition"):
       write = False
       writelines.append("\n")
       
    if line.startswith("Title"):       
       write = True
    
    if write:
         writelines.append(line + " ")

outF.writelines(writelines)  
outF.close()
Jonathan Scholbach
  • 4,925
  • 3
  • 23
  • 44
  • This would only ever give you the first title/condition – Sayse Jan 29 '21 at 12:16
  • @Sayse It was my understanding that Title-lines should be added until the first condition line appears. The question's formatting is a bit misleading, but "Title Lorem ipsum dolor sit amet, consectetur adipiscing" is only one line in the example. – Jonathan Scholbach Jan 29 '21 at 12:17
  • The result output the op has included in their question would suggest they expect to find two matches – Sayse Jan 29 '21 at 12:18
  • 1
    @Sayse OK. I might have misinterpreted the question. Added solutions for both cases. – Jonathan Scholbach Jan 29 '21 at 12:23
  • 1
    @jonathan.scholbach I added a minor change, as its concatenates all lines in a single line. As I want separated lines each time I find 'Title' I put: if line.startswith("condition"): write = False writelines.append("\n") I guess you might have a better solution. I still have the problem that when it appends two lines, the program doesn't put a space, for instance: Title Lorem ipsum dolor sit amet, consectetur adipiscingelit, sed do Title Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do Do you know how to solve this? – nekovolta Jan 29 '21 at 16:36
  • @nekovolta OK, thanks for the edit. Regarding the extra space: That's easy, we just add a space when we add the line to the list of `writelines`. I've just updated the answer accordingly. – Jonathan Scholbach Jan 29 '21 at 16:47
  • @jonathan.scholbach Thank you again. I manage to find a similar solution that is the next one: if write: writelines.append(line) writelines.append(space) But yours is much more elegant. I know my questions are very basic, but I really appreciate your support as I am a total beginner with python. I guess that I will have more questions because this was just the first step :) – nekovolta Jan 29 '21 at 17:01
  • @nekovolta It's fine. We all have been beginners in the beginning. Happy I could be of help :) It is not part of the question, but you might also be interested in the open-context. See https://docs.python.org/3/library/contextlib.html and https://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement – Jonathan Scholbach Jan 29 '21 at 17:37