I want to get those lines that have strings from res list (at least 4 of them , o an O are substitute for zero), along with the names. With script below I get new lines without names, only if line has 0 and 1.
I have a text file with lines of names and numbers as below:
lines in txt file:
John Johns 1-1, 1-2
Adam Adams 1:0, 2:0
Dave Davis 1-0, 1:1
Jim Jims 1_0 1_1
Tim Tims 0 0 0 1
Tom Toms 2-0, 3:2
Pet Peters 1 0 1 1
Sam Sams 1.o 1.1
Ace Aces 10 11
Abe Abes 1O 11
res = ['1', '0', 'o', 'O', '1', '1']
with open('txt_file.txt') as oldfile, open('new_txt.txt', 'w') as newfile:
for x in res:
for line in oldfile:
line_new = [x for x in res if (x in line)]
line = ''.join(line_new)
newfile.write(line)
Expected lines in new file
Dave Davis 1-0, 1:1
Jim Jims 1_0 1_1
Pet Peters 1 0 1 1
Sam Sams 1.o 1.1
Ace Aces 10 11
Abe Abes 1O 11