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>You could consider using something like:</p>

<pre><code>import re
from collections import defaultdict

counts = defaultdict(int)  # zero if key doesn't exists

for line in file:
    start = re.fullmatch('^Start([AB])Check\n$', line).groups()[0]
    end = re.fullmatch('^End([AB])Check\n$', line).groups()[0]
    if start:
        curr_key = group
    elif end:
        assert curr_key == group, "ending line {} doesn't match with an opening line for {}".format(line, curr_key)
        curr_key = None
    else:  # it's a normal line
        counts[curr_key] += 1
</code></pre>

<p>Bonus point: detect non-matching start-end lines + count lines outside start-end lines.</p>

<h1>Without defaultdict</h1>

<p>Replace <code>else</code> clause by:</p>

<pre><code>    else:  # it's a normal line
        if curr_key in counts:
            counts[curr_key] += 1
        else:
            counts[curr_key] = 1
</code></pre>

<p>And define <code>counts</code> as a regular dict:</p>

<pre><code>counts = {}
</code></pre>

<h1>Fixing the given code</h1>

<p>Given code seems working:</p>

<p>Here is a (apparently valid) file definition:</p>

<pre><code>FILE = iter((  # generator of lines
    '-StartACheck',
    'a',
    'b',
    'c',
    '-EndACheck',
    '-StartBCheck',
    'a',
    'b',
    '-EndBCheck',
))
</code></pre>

<p>Here is the missing definitions:</p>

<pre><code>import re
z = {}
</code></pre>

<p>And the provided code:</p>

<pre><code>count=0
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>Output is:</p>

<pre><code>A 4
B 3
</code></pre>

<p>Which is accurate, as the first line (<code>StartACheck</code>) is counted:</p>

<pre><code>      if s:
           e=s.group(1)
           for line in FILE:
               z.setdefault(e,[]).append(count)  # first called with the Start line
</code></pre>

<hr>

<p>Error could be around the file lines extraction : if the file is read as:</p>

<pre><code>file = tuple(open('filename.ext'))
</code></pre>

<p>Then the double for-loop of the source code iterates over each line of the file for each line of the file. Example:</p>

<pre><code>filelines = (1, 2, 3, 4)
for line in filelines:
    for line in filelines:
        print(line)
</code></pre>

<p>And the (valid in this case) almost identical:</p>

<pre><code>filelines = iter((1, 2, 3, 4))
for line in filelines:
    for line in filelines:
        print(line)
</code></pre>

python regex