Python Forum
Find lines from one file in another - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Find lines from one file in another (/thread-30794.html)



Find lines from one file in another - tester_V - Nov-05-2020

Hi, kind of simple thing but for some reason I’m struggling with the iterations Wall in python and the indentations Wall where it stars/end is still very confusing to me.
I have two files.
One is a CSV file that contains names and Mac addresses
The second file also has MAC addresses and some other stuff.

I’m trying to find if the second file MAC addresses are in the First file.
Here is a code I have but for some reason it doesn't iterate.

cur_stat_macs = 'C:/Scripts/All_MAC_Files-TESTING-File.txt'
csv_file_macs = 'C:/Scripts/MACs.csv'

with open (cur_stat_macs,'r') as cur_macs, open (csv_file_macs,'r') as csv_macs :
    
    for current in cur_macs :
        current=current.rstrip()
        current=current.split(",")
        current[2]=current[2].rstrip()

        for csv in csv_macs :
            csv=csv.rstrip()
            if  current[2] in csv :
                print ("Line found  -->> "+csv)
            else :
                print ("Not Found --- >>"+csv)
Thank you.


RE: Find lines from one file in another - tester_V - Nov-06-2020

Any ideas?


RE: Find lines from one file in another - bowlofred - Nov-06-2020

Suggestion, read all the MAC addresses from one file into a set.

Then compare the addresses in the other file to see if they are in the set. If it is, print it.

file1 = "Alpha Bravo Charlie Delta Echo Foxtrot Golf Hotel India Juliet Kilo Lima Mike November Oscar Papa Quebec Romeo Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu"
macs = set()
for mac in file1.split():
    macs.add(mac)

file2 = "Able Baker Charlie Dog Easy Fox George How Item Jig King Love Mike Nan Oboe Peter Queen Roger Sugar Tare Uncle Victor William X-Ray Zebra"
for mac in file2.split():
    if mac in macs:
        print(mac)
Output:
Charlie Mike Victor



RE: Find lines from one file in another - tester_V - Nov-06-2020

bowlofred, Thank you for your help!
I was not totally clear.
I need to use the whole line from the second file.
That is why I'm trying to go with the code I wrote, it identifies the MAC line
current[2]
in
cur_macs
and will search for it in file 2
csv_macs
, if found I need to do something with the line.

with open (cur_stat_macs,'r') as cur_macs, open (csv_file_macs,'r') as csv_macs :
     
    for current in cur_macs :
        current=current.rstrip()
        current=current.split(",")
        current[2]=current[2].rstrip()
 
        for csv in csv_macs :
            csv=csv.rstrip()
            if  current[2] in csv :
                print ("Line found  -->> "+csv)
            else :
                print ("Not Found --- >>"+csv)



RE: Find lines from one file in another - bowlofred - Nov-06-2020

My code was just an example of the algorithm. As we can't see your data, you'll have to parse it. But it seems that's not a problematic part for you.

But what you're doing is trying to read the entire second file every time through the outer loop. Besides being inefficient, this won't work since you only open the file once (you'd have to reopen it to make this work).

Don't nest the loops. Make them separate. On the first loop, store your data in a set. In the second loop, see if the data from the file is in that set.


RE: Find lines from one file in another - deanhystad - Nov-06-2020

I didn't know you could treat a file like a collection this way. For a test file I used a list of teams in the World Series (debug.txt) and created a second file (debug2.txt) that contained Minnesota Twins and Philidelphia Phillies. Then I just printed out the matches
cur_stat_macs = 'debug2.txt'
csv_file_macs = 'debug.txt'
 
with open (cur_stat_macs,'r') as cur_macs, open (csv_file_macs,'r') as csv_macs :
     
    for current in cur_macs :
        current=current.rstrip()
 
        for csv in csv_macs :
            csv=csv.rstrip()
            if  current in csv :
                print (csv)
Output:
1965 Los Angeles Dodgers (NL) Minnesota Twins (AL) 4–3 1987 Minnesota Twins (AL) St. Louis Cardinals (NL) 4–3 1991 Minnesota Twins (AL) Atlanta Braves (NL) 4–3
The program only found matches for the first team in cur_stat_macs. Following Bowlofred's advice I reopen the csv_macs file each time the outer loop is run.
cur_stat_macs = 'debug2.txt'
csv_file_macs = 'debug.txt'
 
with open (cur_stat_macs,'r') as cur_macs :
     
    for current in cur_macs :
        current = current.rstrip()
 
        with open (csv_file_macs,'r') as csv_macs :
            for csv in csv_macs :
                if  current in csv :
                    print (csv.rstrip())
Output:
1965 Los Angeles Dodgers (NL) Minnesota Twins (AL) 4–3 1987 Minnesota Twins (AL) St. Louis Cardinals (NL) 4–3 1991 Minnesota Twins (AL) Atlanta Braves (NL) 4–3 1915 Boston Red Sox (AL) Philadelphia Phillies (NL) 4–1 1950 New York Yankees (AL) Philadelphia Phillies (NL) 4–0 1980 Philadelphia Phillies (NL) Kansas City Royals (AL) 4–2 1983 Baltimore Orioles (AL) Philadelphia Phillies (NL) 4–1 1993 Toronto Blue Jays (AL) Philadelphia Phillies (NL) 4–2 2008 Philadelphia Phillies (NL) Tampa Bay Rays (AL) 4–1 2009 New York Yankees (AL) Philadelphia Phillies (NL) 4–2
This found all the teams in the cur_stat_macs file. Also following Bowlofred's advice I rewrote the code to first read in the cur_stat_macs file and saved it in a list. Then I read the csv_file_macs fiile and checked each saved entry against each line. I measured the execution time to see if this is faster.
import time

start_time = time.perf_counter()

cur_stat_macs = 'debug2.txt'
csv_file_macs = 'debug.txt'

teams = []
with open (cur_stat_macs,'r') as cur_macs :
    teams = [team for team in cur_macs]
 
with open (csv_file_macs,'r') as csv_macs :
    for csv in csv_macs :
        for team in teams:
            if team in csv:
                print (csv.rstrip())
                break;

print(time.perf_counter() - start_time)
Time for this saved entries version was 0.0526529 seconds. When I timed the version that rescans the csv_file_macs over and over the time was 0.0745832 seconds. Not much different, around 25%, but this may will grow very fast as then number of entries in the cur_stat_macs file increases (my file only has 2 entries).


RE: Find lines from one file in another - tester_V - Nov-14-2020

I really appreciate your help! Smile

I see your point but your code does not have an "else" statement.
I need it, I need to process lines that do not match.
I can print lines that are matched. The iteration in Python is a horror story. Wall Wall Wall
I'd like to repeat, I do not have(by now) finding or matching lines, I have a problem printing those that do not match.
Here is a try 234 of the script:

fout = open ('C:/Scripts/sometext.txt','w')        

with open ('C:/Scripts/Python/CurMACs.txt','r') as cur_fl :        
    cur2 = cur_fl.readlines()
with open ('C:/Scripts/Python/Matched_MACs.txt','r') as m_file :     

    for m_line in m_file :
        m_line = m_line.rstrip()       
        sp_m_line = m_line.split(",")
        m_ltof = sp_m_line[0]+","+sp_m_line[1]  
        for cln2 in cur2 :
            cln2=cln2.rstrip()
        #    print ("Current Line file -->> ",cln2)
            if m_ltof in cln2 :
                #print ('Found Matched line -- ',cln2)
                new_n = cln2+","+"Online"
                print (new_n)
                fout.write(new_n+'\n')
                break

               # else :
               #     print ("OFFLINE !!!!!",cln2)
               #     #new_off= cln2+","+"OffLine"
               #     #fout.write(new_off+'\n')
               #     break
fout.close()
I tried to move the 'else' block all over the place wrote a countless number of snippets and still cannot print the lines that do not match..


RE: Find lines from one file in another - deanhystad - Nov-14-2020

Look at any or for...else


RE: Find lines from one file in another - tester_V - Nov-15-2020

(Nov-14-2020, 05:23 AM)deanhystad Wrote: Look at any or for...else

Thank you deanhystad!
You are right!
I fixed the code and the 'else:' works now.