Python Forum
Find lines from one file in another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find lines from one file in another
#1
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.
Reply
#2
Any ideas?
Reply
#3
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
Reply
#4
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)
Reply
#5
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.
Reply
#6
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).
tester_V likes this post
Reply
#7
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..
Reply
#8
Look at any or for...else
tester_V likes this post
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  FileNotFoundError: [WinError 2] The system cannot find the file specified NewBiee 2 1,562 Jul-31-2023, 11:42 AM
Last Post: deanhystad
  Cannot find py credentials file standenman 5 1,634 Feb-25-2023, 08:30 PM
Last Post: Jeff900
  selenium can't find a file in my desk ? SouAmego22 0 739 Feb-14-2023, 03:21 PM
Last Post: SouAmego22
  Find (each) element from a list in a file tester_V 3 1,205 Nov-15-2022, 08:40 PM
Last Post: tester_V
  what will be the best way to find data in txt file? korenron 2 1,151 Jul-25-2022, 10:03 AM
Last Post: korenron
  Delete multiple lines from txt file Lky 6 2,286 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  find some word in text list file and a bit change to them RolanRoll 3 1,522 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  failing to print not matched lines from second file tester_V 14 6,078 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,954 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Find and delete above a certain line in text file cubangt 12 3,461 Mar-18-2022, 07:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020