Answers for "python delete duplicates from text file"

1

txt file duplicate line remover python

lines_seen = set() # holds lines already seen

with open("file.txt", "r+") as f:
    d = f.readlines()
    f.seek(0)
    for i in d:
        if i not in lines_seen:
            f.write(i)
            lines_seen.add(i)
    f.truncate()
Posted by: Guest on September-19-2020
3

python delete duplicate lines in file

with open("file.txt", "r") as txt_file:
  new_data = list(set(txt_file))
  return new_data
Posted by: Guest on November-30-2020
0

Python remove duplicate lines from a text file

import hashlib
def main():
    input_file = "in.txt"
    output_file = "out.txt"
    
    completed_hash = set()
    output_file = open(output_file, "w")
    
    for line in open(input_file,"r"):
        hashValue = hashlib.md5(line.strip().encode('utf-8')).hexdigest()
        
        if hashValue not in completed_hash:
            output_file.write(line)
            completed_hash.add(hashValue)
            
    output_file.close()


if __name__ == "__main__":
    main()
Posted by: Guest on March-16-2022

Code answers related to "python delete duplicates from text file"

Python Answers by Framework

Browse Popular Code Answers by Language