Regular expression in python
<p>I have text file (seq.fasta) which contains sequence as follows</p>

<pre><code>M1

MPMILGYWNVRGLTHPIRMLLEYTDSSYDEKRYTMGDAPDFDRSQWLNEKFKLGLDFPNL
PYLIDGSHKITQSNAILRYLARKHHLDGETEEERIRADIVENQVMDTRMQLIMLCYNPDF
EKQKPEFLKTIPEKMKLYSEFLGKRPWFAGDKVTYVDFLAYDILDQYRMFEPKCLDAFPN
LRDFLARFEGLKKISAYMKSSRYIATPIFSKMAHWSNK
</code></pre>

<p>I have to extract motif PXXP exactly 4 characters (XX can be any characters).</p>

<p>I tried following code:</p>

<pre><code>import re

infile=open("seq.fasta",'r')

out=open("out.csv",'w')

for line in infile:

   line = line.strip("\n")

   if line.startswith('&gt;'):

      name=line

   else:

      motif = re.compile(r"(\bP{2}P\b)")

      c = line.count('motif')

      print '%s:%s' %(name,c)

      out.write('%s:%s\n' %(name,c))
</code></pre>

<p>But it is not finding motif.</p>
