Good day to all, please tell me how to make a comparison and removal. First file format

Y.Turner@Hotmail.Com:Yvette Turner Kelliekelly@Hotmail.Com:Kellie Kelly Hades2056@Hotmail.Com:James Gillam Peter.Garrad@Tesco.Net:Carl Garrad 

Second format

 Hades2056@Hotmail.Com Peter.Garrad@Tesco.Net 

That is, in the second file, purely soapboxes, the goal is to remove lines from the first file that contain soap from the second. Unfortunately, the python just started to learn, tell me what to cling to.

  • You have a problem in that you do not know how to write it in a specific language, or you can not think of an algorithm? - iksuy
  • Are you not working for spammers? - VladD

3 answers 3

 file_1 = open('1.txt', 'r').read().split('\n') file_2 = open('2.txt', 'r').read().split('\n') array = [] for email in file_1: if email.split(':')[0] not in file_2: array.append(email) str = "" for email in array: str += email str += '\n' file_3 = open('3.txt', 'w') file_3.write(str) 

Thank you undeadter

    You can do with one cycle:

     import csv fn1 = r'C:\Temp\.data\616780_1.txt' fn2 = r'C:\Temp\.data\616780_2.txt' with open(fn1) as f1, open(fn1 + '.new', 'w') as fout, open(fn2) as f2: mails = set(f2.read().splitlines()) csv_writer = csv.writer(fout, delimiter=':', quoting=csv.QUOTE_MINIMAL) for row in csv.reader(f1, delimiter=':'): if not row[0] in mails: csv_writer.writerow(row) 

    C: \ Temp.data \ 616780_1.txt.new:

     Y.Turner@Hotmail.Com:Yvette Turner Kelliekelly@Hotmail.Com:Kellie Kelly 

      To remove lines from a 1.txt file that begin with an e-mail from a 2.txt file:

       #!/usr/bin/env python3 from pathlib import Path from fileinput import FileInput prefixes = tuple(email+':' for email in Path('2.txt').read_text().splitlines()) with FileInput('1.txt', inplace=True, backup='.bak') as file: for line in file: if not line.startswith(prefixes): print(line, end='') # keep 

      Result

       Y.Turner@Hotmail.Com:Yvette Turner Kelliekelly@Hotmail.Com:Kellie Kelly