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 :: python join list to string 
Python :: remove emoji from dataframe 
Python :: discord.py get profile picture 
Python :: pandas strips spaces in dataframe 
Python :: logging in with selenium 
Python :: order dictionary by value python 
Python :: how to make a pythoon turtle follow another? 
Python :: python get dict values as list 
Python :: python read zipfile 
Python :: extract filename from path in python 
Python :: auto python to exe 
Python :: find a file in python 
Python :: Django - include app urls 
Python :: json python no whitespace 
Python :: turtle write 
Python :: discord.py how to use permissions 
Python :: Get a random joke in python 
Python :: discord.py get guild member list 
Python :: selectfield flask wtf 
Python :: flask redirect to url 
Python :: save a file as a pickle 
Python :: how to append data to csv file in python without replacing the already present text 
Python :: Learn python 3 the hard way by by Zed Shaw 
Python :: python program to print prime numbers in an interval 
Python :: await async function from non async python 
Python :: replace number with string python 
Python :: pyqt5 qlineedit on change 
Python :: python folder exists 
Python :: python3 yyyymmddhhmmss 
Python :: convert image to grayscale opencv 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =