Find the same pattern in two files python
Question: Im trying to find a word that is in a file and search for that word in two
different files \- to search that word in the first file and if the word is
found the the line is printed in a new file \- to search for the same word in
a second file and if the word is found to print the line found in the second
file in the same output file after the line found in the first file and
separated with a _tabulation._

this is the script that i wrote :

    
    
    for line in first_file:
       for line2 in second_file:
          for word in keywords:
           if re.match (r"\b"+word+r"\b" , line):
              result = (line.strip()+'\t'+line2.strip())
              print (result, file = new_file)
    

but the result that i get is always an empty file.

    
    
    This is an example of the first file : 
    contig-39000000 1211    11.3902
    contig-44000000 6128    17.8944
    contig-91000000 2180    14.2197
    
    this is an example of the second file 
    contig-316000330    out.18
    contig-39000000 out.25
    

...

and this is an example of the search file

    
    
    contig-39000000
    contig-289000379
    contig-300000489
    contig-310000168
    contig-310000172
    contig-320000463
    contig-324000213
    contig-327000055
    contig-341000436
    contig-342000384
    ....
    

The expected result would look like :

    
    
    contig-39000000 1211    11.3902   out.25
    

Thank you


Answer: Yeah, pandas was much easier than SQLite.

    
    
    import pandas as pd
    
    # change sep to use '\t' if those files actually are tab-delimited 
    df1 = pd.read_csv('file1.txt', sep=r'\s+', header=None)
    df2 = pd.read_csv('file2.txt', sep=r'\s+', header=None)
    
    result = pd.merge(df1, df2, on=0)
    result.to_csv('output.txt', sep='\t', header=False, index=False)
    

* * *

Contents of `file1.txt`

    
    
    contig-39000000 1211    11.3902
    contig-44000000 6128    17.8944
    contig-91000000 2180    14.2197
    

Contents of `file2.txt`

    
    
    contig-39000000     out.13
    contig-316000330    out.18
    contig-316000341    out.25
    

Contents of `output.txt`

    
    
    contig-39000000 1211    11.3902 out.13
    

