1

My log file looks like this:

-------------
name=ABC
age=12
DOB=02/12/2001
EOL
-------------
name=DEF
age=9
DOB=06/20/2005
EOL
-------------
name=XYZ
age=23
DOB=02/12/1992
EOL

How can I read the file one section at a time? i.e. from the "---" to "EOL".

Tushar
  • 11
  • 1
  • 1
    Possible duplicate of [Splitting large text file by a delimiter in Python](http://stackoverflow.com/questions/7980288/splitting-large-text-file-by-a-delimiter-in-python) – chickity china chinese chicken May 17 '17 at 01:20
  • Possible duplicate of [Splitting large text file by a delimiter in Python](http://stackoverflow.com/questions/7980288/splitting-large-text-file-by-a-delimiter-in-python) – polka May 17 '17 at 01:48

1 Answers1

0

Here's a python example that's battle-tested with your input (pasted into "input.txt"):

 sections = []

 with open("input.txt") as file:
     section = ""
     for line in file.readlines():
         if line.strip() == "-------------":
             sections.append(section)
             section = ""
         else:
             section += line + "\n"
        print(sections)

Now you can iterate through the sections list and do whatever you want to each setion

Nick Weseman
  • 1,502
  • 3
  • 16
  • 22
  • Thanks! And how can I use regex matching to extract the names and other details from each section? – Tushar May 17 '17 at 01:47
  • You can just go through each line in the `section` and `if section.startswith('name'): `. Do this for each property. – Nick Weseman May 17 '17 at 01:49