Skip to content Skip to sidebar Skip to footer

How Can I Get Only Heading Names.from The Text File

I have a Text file as below: Education: askdjbnakjfbuisbrkjsbvxcnbvfiuregifuksbkvjb.iasgiufdsegiyvskjdfbsldfgd Technical skills : java,j2ee etc., work done: oaugafiuadgkfjw

Solution 1:

To get just the headings from your text file, you could use the follows:

import re

withopen('aks.txt') as f_input:
    headings = re.findall(r'(.*?)\s*:', f_input.read())
    print headings

This would display the following:

['Education', 'Technical skills', 'work done']

Solution 2:

If you are sure that the title names occure before a colon (:) then you can write a regex to search for such a pattern.

import re
    withopen("aks.txt") as infile:
      for s in re.finditer(r'(?<=\n).*?(?=:)',infile.read()):
        print s.group()

The output will be like

   Education
   Technical skills 
   work done

Post a Comment for "How Can I Get Only Heading Names.from The Text File"