dropdown menu

Back To Home

Thursday, November 14, 2019

Python- match character and delete it

Matched 7 in reference file and delete from main file.

Python code:
                              
infile = "yourfile.txt"
outfile = "yourfile2.txt"

with open("file2.txt", "r") as f:
    delete_list = f.readlines()

fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
    
    for word in delete_list:
        
        line = line.replace(word, "")
        print(line)
    fout.writelines(line)

fin.close()
fout.close()

file2:
7

yourfile:
1
2
3
4
5
6
7
8

yourfile2:
1
2
3
4
5
6

8



                                       

No comments:

Post a Comment