Python Forum
failing to print not matched lines from second file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
failing to print not matched lines from second file
#1
Greetings!
For some reason, I do not get the iterations in Python. Sad
Especially with "Else". Confused
here is a problem, I have two files, File-1 and File-2. I must use File-1 lines to find lines in File-2.
I have no problem finding lines but I cannot print Lines that do not match (from File-2)

File-1 lines:
03/28/2021,P,6,LINE2
03/28/2021,P,9,LINE4

File-2 lines:
03/28/2021,P,16,LINE1
03/28/2021,P,6,LINE2
03/28/2021,P,9,LINE3
03/28/2021,P,9,LINE4
03/28/2021,P,8,LINE5
03/28/2021,S,95,LINE6
03/28/2021,S,1,LINE7
03/28/2021,P,46,LINE8

I need to print out only lines that do not match:
03/28/2021,P,16,LINE1
03/28/2021,P,9,LINE3
03/28/2021,P,8,LINE5
03/28/2021,S,95,LINE6
03/28/2021,S,1,LINE7
03/28/2021,P,46,LINE8

But the code I wrote prints this:
ELSE -->> 03/28/2021,P,16,LINE1
ELSE -->> 03/28/2021,P,9,LINE3
ELSE -->> 03/28/2021,P,9,LINE4
ELSE -->> 03/28/2021,P,8,LINE5
ELSE -->> 03/28/2021,S,95,LINE6
ELSE -->> 03/28/2021,S,1,LINE7
ELSE -->> 03/28/2021,P,46,LINE8
ELSE -->> 03/28/2021,P,16,LINE1
ELSE -->> 03/28/2021,P,6,LINE2
ELSE -->> 03/28/2021,P,9,LINE3
ELSE -->> 03/28/2021,P,8,LINE5
ELSE -->> 03/28/2021,S,95,LINE6
ELSE -->> 03/28/2021,S,1,LINE7
ELSE -->> 03/28/2021,P,46,LINE8

I have to split lines in File-1 and File-2, some additional processing required in the lines.
Here is the code:
with open (file_2,'r') as l_few :
    f2=l_few.readlines() 
with open (file_1,'r') as f1:          
    for lf1 in f1:
        lf1=lf1.strip()
        sp1 = lf1.split(",")
        for lf2 in f2 :
            lf2=lf2.strip()
            if lf2 :
                if sp1[3] in lf2 : 
                    spL2=lf2.split(",")
                    #print (" File 2 Line matched --> "+lf2)
                    #break
                else :
                    print (" ELSE -->> "+lf2)
                    #break
Any help appreciated, I exhausted my resources, trying to solve it for the last 3 days. Sick
I tried to move "else" all over the place and used "break" but still cannot make a clean print of only the 'Not Matched' lines.
Thank you.
Reply


Messages In This Thread
failing to print not matched lines from second file - by tester_V - Mar-29-2021, 04:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to connect by 'net use' tester_V 1 226 Apr-20-2024, 06:31 AM
Last Post: tester_V
  Failing to print sorted files tester_V 4 1,324 Nov-12-2022, 06:49 PM
Last Post: tester_V
  Saving the print result in a text file Calli 8 1,908 Sep-25-2022, 06:38 PM
Last Post: snippsat
  Failing reading a file and cannot exit it... tester_V 8 1,865 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Failing regex tester_V 3 1,232 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Delete multiple lines from txt file Lky 6 2,375 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Print to a New Line when Appending File DaveG 0 1,247 Mar-30-2022, 04:14 AM
Last Post: DaveG
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,129 Mar-28-2022, 03:38 PM
Last Post: snippsat
Sad Want to Save Print output in csv file Rasedul 5 11,140 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Convert legacy print file to XLSX file davidm 1 1,836 Oct-17-2021, 05:08 AM
Last Post: davidm

Forum Jump:

User Panel Messages

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