title: matching parentheses using a file
body: <p>I am trying to match parentheses from a line in a file but when I use the code below without getting data from a file and entering it instead it works and matched the parentheses. I don't know how to allow it to work with numbers and letters in between too. </p>
  
  <p>i have tried many different ways but this has worked the best so far. I think there is firstly something wrong with what i am printing but i have tried everything that i know to fix that. i am also new to python so it might not be the best code. </p>
  
  <pre><code>class Stack:
      def __init__(self):
          self._items = []
  
      def isEmpty(self):
          return self._items == []
  
      def push(self,item):
          self._items.append(item)
  
      def pop(self):
          return self._items.pop()
  
  stack = Stack()
  
  open_list = ["[","{","("] 
  close_list = ["]","}",")"] 
  
  def open_file():
      file = open("testing.txt","r")
      testline = file.readline()
      count = 1
      while testline != "":
          testline = testline[:-1]
          check(testline,count)
          testline = file.readline()
          count = count + 1
  
  
  def check(testline,count): 
      stack = [] 
      for i in testline:
          if i in open_list: 
              stack.append(i) 
          elif i in close_list: 
              pos = close_list.index(i) 
              if ((len(stack) &gt; 0) and
                  (open_list[pos] == stack[len(stack)-1])): 
                  stack.pop() 
              else: 
                  print ("Unbalanced")
                  print (count)
      if len(stack) == 0: 
          print ("Balanced")
          print (count)
  
  
  def main():
      open_file()
  
  if __name__=="__main__": 
      main()
  </code></pre>
  
  <p>output:</p>
  
  <p>if the file contains </p>
  
  <p>dsf(hkhk[khh])</p>
  
  <p>ea{jhkjh[}}</p>
  
  <p>hksh[{(]</p>
  
  <p>sd{hkh{hkhk[hkh]}}]</p>
  
  <p>the output is</p>
  
  <pre><code>Balanced
  1
  Unbalanced
  2
  Unbalanced
  2
  Unbalanced
  3
  Unbalanced
  4
  Balanced
  4
  </code></pre>
  
  <p>The first four are correct but it adds 2 and i have no idea where it is coming from. I need the count for later purposes when i am printing (ie line 1 is balanced)</p>