title: How to write a text file to CSV based on appearance of keyword
body: <p>My text file looks like this. It is o/p from some program</p>
  <pre><code>**********End Transcription Grammar correction log***********
  Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.29k/1.29k [00:00&lt;00:00, 480kB/s]
  Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 850M/850M [00:30&lt;00:00, 29.0MB/s]
  Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 773k/773k [00:00&lt;00:00, 3.66MB/s]
  Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.32M/1.32M [00:00&lt;00:00, 7.99MB/s]
  Line # 1
  I settled the first question: &quot;I have fit you n if you could choose one word to describe your experience of being pregnant, what would it be otherwise it would be lucky.&quot;
  Line # 2
  A because I had several pregnancy losses I'm prior to conceiving my daughter, and then this permetsii and I'm thinking about you, how do you think that things will change once you?
  Line # 3
  become apparent again Amen I (Thinka) having two children is going to be a lot more busy I'm going to have to really focus on staying organized a because it's hard enough.
  Line # 4
  With a toddler, O and my husband and I are going to really have to work as a team we already do, but it's going to take it to a different level.
  Line # 5
  SM O Andte Have you thought about things like breast eating, sleeping arrangements or child cae, yes, and a if you could elaborate on is O sura so I planned to you.
  Line # 6
  Nursing and My Daughter: My nurse to my my M toddler until she was about nine months UM and I plan to try to go as long as I can with this.
  </code></pre>
  <p>what I was trying to do is saving the this <code>content</code> in a <code>csv</code> file based on condation that it should look lile this....</p>
  <pre><code>num           text
  
  Line #1       I settled the first question: &quot;I have fit you n if you could choose one word to 
                describe your experience of being pregnant, what would it be otherwise it would be lucky.&quot;
  
  
  Line #2      A because I had several pregnancy losses I'm prior to conceiving my daughter, 
                    and then this permetsii and I'm thinking about you, how do you think that 
                things will change once you?
  
  ''''........................
  Line n       some text
  </code></pre>
  <p>code I've tried</p>
  <pre><code>content = ''
  with open(r&quot;output.txt&quot;, 'r') as txt_file:
    content = txt_file.read()
  
  blocks = content.split('Line #')
  
  csv_content = ''
  for block in blocks:
    if block != '':
      csv_content += 'Line # %s\n' % ' | '.join(block.splitlines())
      print(csv_content)
  
  with open('csv_file.csv', 'w') as csv_file:
    csv_file.write(csv_content)
  </code></pre>
  <p>output I got</p>
  <p><a href="https://i.stack.imgur.com/c7l2U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c7l2U.png" alt="enter image description here" /></a></p>
  <p>output not even splitting for keyword I want <code>Line#</code>. Am i missing anything here?. Any suggestions would be helpful</p>