Fetch particular text from another file (python)
<p>I have a file of the following pattern:</p>

<pre><code>"abcd.asxs." "alphabets"
"wedf.345.po&amp;%12." "numbers"
"xyhd.iu*u." "characters"
"megaten4.koryaku-memo.xyz." "alphabets"
"adwdbk.uyequ." "alphabets"
"233432.2321." "numbers"
"tytqyw.sdfhgwq." "alphabets"
</code></pre>

<p>I want something like:</p>

<pre><code>string[0]=abcd.asxs
string[1]=megaten4.koryaku-memo.xyz
string[2]=tytqyw.sdfhgwq
and so on....
</code></pre>

<p>What code I have written is:</p>

<pre><code> #!/usr/bin/python

 import re

 important = []
 needed_categories = ["alphabets"]

 with open('file.txt') as fp:
        rec=fp.readlines()

 for line in rec:
        for category in needed_categories:
                if category in line:
                        important.append(line)
                        break
 print("\n".join(important))
</code></pre>

<p>Output I get:</p>

<p>"abcd.asxs." "alphabets"</p>

<p>"megaten4.koryaku-memo.xy." "alphabets"</p>

<p>"tytqyw.sdfhgwq." "alphabets"</p>

<p>change <code>important.append(line)</code> to:</p>

<p><code>
if line.strip().endswith('"alphabets"'):
  important.append(line.split(' ')[0].strip('"').strip('''))
</code></p>

python