Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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()
Comment

python delete duplicate lines in file

with open("file.txt", "r") as txt_file:
  new_data = list(set(txt_file))
  return new_data
Comment

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()
Comment

PREVIOUS NEXT
Code Example
Python :: relative frequency histogram python 
Python :: controlling the mouse with pynput 
Python :: python screeninfo 
Python :: Total processing python 
Python :: python docker 
Python :: check this id exist in database django 
Python :: string remove suffix python 
Python :: python child class call parent method 
Python :: python different types of loops 
Python :: Python Join Lists 
Python :: remove element from list python by value 
Python :: python language server 
Python :: python ide 
Python :: best way to access nested key in python 
Python :: insert an element in list python 
Python :: parse xml in python 
Python :: import one file into another python 
Python :: python equivalent of R sample function 
Python :: python how to make boxplots with swarmplot 
Python :: sorted string 
Python :: torch.stack 
Python :: numpy roll 
Python :: Python Dynamic Create var 
Python :: select statement python 
Python :: 2nd to last index python 
Python :: python first 
Python :: Python re.subn() 
Python :: binary search tree in python 
Python :: python capitalize 
Python :: Convert csv to dictionary in Python 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =