Parsing with regex
<p>I'm trying to count the number of lines contained by a file that looks like this:</p>

<pre><code>-StartACheck
---Lines--
-EndACheck
-StartBCheck
---Lines--
-EndBCheck
</code></pre>

<p>with this:</p>

<pre><code>count=0
z={}
for line in file:
      s=re.search(r'\-+Start([A-Za-z0-9]+)Check',line)
      if s:
           e=s.group(1)
           for line in file:
               z.setdefault(e,[]).append(count)
               q=re.search(r'\-+End',line)
               if q:
                   count=0
                   break

for a,b in z.items():
    print(a,len(b))
</code></pre>

<p>I want to basically store the number of lines present inside ACheck , BCheck etc in a dictionary but I keep getting the wrong output</p>

<p>Something like this</p>

<pre><code>A,15
B,9
</code></pre>

<p>etc</p>

<p>Given the pattern of </p>

<pre><code>-StartACheck
--- Line 1
-EndACheck
-StartBCheck
---Line 1
-EndBCheck
-StartACheck
---Line 1
---Line 2
---Line 3
-EndACheck
</code></pre>

<p>You can use a multi-line regex to capture the blocks that start with <code>-Start[pattern]Check</code> and end with <code>-End[pattern]Check</code> with something like this:</p>

<pre><code>^-Start([A-Za-z0-9]+)Check$(.*?)^-End(?:\1)Check$
</code></pre>

<p><a href="https://regex101.com/r/oD8aW5/3" rel="nofollow">Demo</a></p>

<p>In Python, you can combine that with <code>re.finditer</code> and a <code>Counter</code> like so:</p>

<pre><code>import re
from collections import Counter
pattern=r'^-Start([A-Za-z0-9]+)Check$(.*?)^-End(?:\1)Check$'

c=Counter()
with open(fn, "r") as f:
    for m in re.finditer(pattern, f.read(), re.S | re.M):
        c+=Counter({m.group(1): len(m.group(2).splitlines())-1})
</code></pre>

<p>Prints:</p>

<pre><code>Counter({'A': 4, 'B': 1})
</code></pre>

<p>If you are concerned about reading the entire file into memory, use a <code>mmap</code> of the file like so:</p>

<pre><code>import re
from collections import Counter
import mmap
pattern=r'^-Start([A-Za-z0-9]+)Check$(.*?)^-End(?:\1)Check$'

c=Counter()
with open(fn, "r+") as f:
    mm=mmap.mmap(f.fileno(), 0)
    for m in re.finditer(pattern, mm, re.S | re.M):
        c+=Counter({m.group(1): len(m.group(2).splitlines())-1})
</code></pre>

<p>Then the OS will manage reading the file in appropriate chunks to match the regex. </p>

python regex