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
#8
(Mar-29-2021, 11:27 AM)Serafim Wrote: with open ('file_2','r') as l_few :    f2=l_few.readlines()sp23 = [s.split(',')[3].strip() for s in f2] # collect all the terms you need to comparewith open ('file_1','r') as f1:              for lf1 in f1:        lf1=lf1.strip()        sp1 = lf1.split(",")        if sp1[3] in sp23:            pass # here we have a match so print nothing            # spL2=lf2.split(",")            #print (" File 2 Line matched --> "+lf2)            #break        else :            print (lf1) # print the non-matching line            #break

My bad! the code s actually working!
Thank you very much for your help!

I made some insignificant changes to your code, just easier to read (for me).

with open (file_1,'r') as l_few :
    f1=l_few.readlines()
for s in f1 :
    sp23 = s.split(',')

with open (file_2,'r') as f2:          
    for lf2 in f2:
        lf2=lf2.strip()
        sp2 = lf2.split(",")
        if sp2[3] in sp23:
            pass # here we have a match so print nothing
        else :
            print (lf2) # print the non-matching line
            #break
Reply


Messages In This Thread
RE: failing to print not matched lines from second file - by tester_V - Mar-29-2021, 09:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Failing to connect by 'net use' tester_V 1 233 Apr-20-2024, 06:31 AM
Last Post: tester_V
  Failing to print sorted files tester_V 4 1,326 Nov-12-2022, 06:49 PM
Last Post: tester_V
  Saving the print result in a text file Calli 8 1,917 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,233 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Delete multiple lines from txt file Lky 6 2,379 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,131 Mar-28-2022, 03:38 PM
Last Post: snippsat
Sad Want to Save Print output in csv file Rasedul 5 11,155 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