title: Using python to treat a .txt file as a large string, and iterate over each {} in the file
body: <p>I have a data.txt and a count.py.</p>
  
  <p>My data in data.txt looks something like this:</p>
  
  <pre><code>hermione [
    {
     keyword.adsf
     keyword.bsdf
    },
    {
     keyword.1sdf
     keyword.bsd
     keyword.bsd
    }
    ]
  ron [
    {
     keyword.adsf
     keyword.bsdf
    },
    {
     keyword.1sdf
     keyword.bsd
     keyword.bsd
    }
    ]
  </code></pre>
  
  <p>What I want to do is count the number of <code>keyword.xxx</code> occurrences inside each {} using python. In other words, I want my output to be like this:</p>
  
  <pre><code>hermione [
    {
     2
    },
    {
     3
    }
    ]
  ron [
    {
     2
    },
    {
     3
    }
    ]
  </code></pre>
  
  <p>I was thinking that in count.py, I would write the script to make the counts, and treat data.txt as a large string. </p>
  
  <p>So far this is the code that I've written:</p>
  
  <pre><code>from sys import argv
  script, filename = argv
  txt = open(filename).read()
  def count_in_bracket():
      print txt
  print count_in_bracket()
  </code></pre>
  
  <p>(I run python <code>custom_fields_nocount.py custom_fields_nocount.txt</code> in the terminal.)</p>
  
  <p>... which isn't much, since it doesn't iterate through each {} bracket. </p>
  
  <p>That's the part I have trouble figuring out. How do I write something like</p>
  
  <pre><code>list = ['ron', 'hermione']
  for {} in list:
      print len(CONTENTS_OF_EACH_{}.split("keyword"))-1
  </code></pre>
  
  <p>?</p>