Extraction of ssn no., date, e-mail address from a file using Python
<p>1.<strong>I have a file named <code>rexp.txt</code> with the following content:</strong></p>

<pre><code>adf fdsf hh  h fg h 1995-11-23
dasvsbh 2000-04-12 gnym,mnbv 2001-02-17
dascvfbsn
bjhmndgfh
xgfdjnfhm244-44-2255  fgfdsg gfjhkh
fsgfdh 455-44-6577 dkjgjfkld
sgf
dgfdhj 
sdg 192.6.8.02 fdhdlk dfnfghr
fisdhfih dfhghihg 154.56.2.6 fdhusdgv
aff fjhgdf 
fdfdnfjgkpg
 fdf hgj  fdnbk gjdhgj 

dfdfg raeh95@gmail.com efhidhg  fdfuga reg@gmail.com
ergudfi rey@gmail.com iugftudfh dgufidjfdg
teeeee@gmail.comugfuhlfhs fgufif p
</code></pre>

<p>2.I want to extract the ssn number, date, e-mail line by line. I'm expecting code that loops through every line and returns the expected strings. </p>

<p>3.<strong>Correct the coding in Python</strong>:</p>

<pre><code>import re
def cfor_date(str):
    t=re.search(r'(\d{4}-\d{2}-\d{2})',str)
    return t

def cfor_ssn(str):
    f=re.search(r'(\d{3}-\d{2}-\d{4})',str)
    return f

def cfor_gm(str):
    g=re.search(r'([\w\.-]+@gmail[\w{3}\.-]+)',str)
    return g

f = open("rexp.txt","r").read()
lines = f.splitlines()
for line in iter(lines):
    x=line.split(" ")
    print x
    if (cfor_date(x)) != None: # i feel problem here
        r=cfor_ssn(x)
        print r
</code></pre>

<ul>
<li>You are opening file, reading it completely, then splitting what is read into list using <code>splitlines()</code> and then iterating over that list. Too much long and complicated process. Also, file is not closed after it was read.</li>
<li>Instead of this, why not open file using <code>with</code> construct and then read file completely using <code>readlines()</code>. No need to split lines and no need to worry of closing file.</li>
<li>In your code, once you started iterating line by line, you are again splitting line on basis of single space and then you are passing the output of split which will be list to you functions to extract date/email/ssn. Here where the problem is.</li>
<li>No need to split line on basis of spaces. Pass the line directly to functions to extract the data.</li>
<li>Your regular expression are good. I didn't modify it.</li>
<li>I have replaced the <code>search</code> function with <code>findall</code> function. Difference between both is explained in below example.</li>
</ul>

<blockquote>
<pre><code> &gt;&gt;&gt; import re
 &gt;&gt;&gt; a = "Dinesh 123"

 &gt;&gt;&gt; t = re.search(r"\d+",a)

 &gt;&gt;&gt; t &lt;_sre.SRE_Match object at 0x01FE3918&gt;

 &gt;&gt;&gt; t.group() 
 &gt;&gt;&gt; '123'


 &gt;&gt;&gt; x = re.findall(r'\d+',a)
 &gt;&gt;&gt; x
 &gt;&gt;&gt; ['123']
</code></pre>
</blockquote>

<p>For more help, check this <a href="https://pymotw.com/2/re/" rel="nofollow">link</a> !!!</p>

<p>All above points are present in below code :</p>

<p><strong>Code:</strong></p>

<pre><code>import re
def cfor_date(tmp_line):
    t=re.findall(r'(\d{4}-\d{2}-\d{2})',tmp_line)
    return t

def cfor_ssn(tmp_line):
    f=re.findall(r'(\d{3}-\d{2}-\d{4})',tmp_line)
    return f

def cfor_gm(tmp_line):
    g=re.findall(r'([\w\.-]+@gmail[\w{3}\.-]+)',tmp_line)
    return g

with open("xyz.txt","r") as fh:
    for line in fh.readlines():
        date_list = cfor_date(line)
        ssn_list = cfor_ssn(line)
        gm_list = cfor_gm(line)

        if len(ssn_list) != 0:
            print ssn_list
        if len(date_list) != 0:
            print date_list
        if len(gm_list) != 0 :
            print gm_list
</code></pre>

<p><strong>Output :</strong></p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;python c.py
['1995-11-23']
['2000-04-12', '2001-02-17']
['244-44-2255']
['455-44-6577']
['raeh95@gmail.com', 'reg@gmail.com']
['rey@gmail.com']
['teeeee@gmail.comugfuhlfhs']

C:\Users\dinesh_pundkar\Desktop&gt;
</code></pre>

python fileparsing