Earlier I wrote the code for extracting a specific string from multiple files and the result is stored in a separate file.Now this file has duplicate results which I need to remove .
import glob
import re
import os.path
path=r"H:\sample"
file_array=glob.glob(os.path.join(path,'*.txt'))
with open("aiq_hits.txt","w") as out_file;
for input_filename in file_array:
with open(input_filename) as in_file:
for line in in_file:
match=re.findall(r"""(?<=')[^']*\.aiq(?=')|(?<=")[^"]*\.aiq(?=")""")
for item in match:
out_file.write("%s\n" %item)
out_file.close()
This out_file has duplicate results which I need to remove and result should be the same file
readlines will return a list of lines from the file content.lines.line.new_lines.line in the new_lines list.new_lines into the file.Demo:
input_file = "input.txt"
with open(input_file, "r") as fp:
lines = fp.readlines()
new_lines = []
for line in lines:
#- Strip white spaces
line = line.strip()
if line not in new_lines:
new_lines.append(line)
output_file = "output.txt"
with open(output_file, "w") as fp:
fp.write("\n".join(new_lines))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With