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 :: plt.imshow not showing 
Python :: remove rows or columns with NaN value 
Python :: flip specific bit python 
Python :: django clear db 
Python :: python json indented 
Python :: print undeline and bold text in python 
Python :: python program to give shop name 
Python :: matplotlib title not fully visible 
Python :: openpyxl delete rows 
Python :: python image black and white 
Python :: python sort dataframe by one column 
Python :: pyhton find dates in weeks 
Python :: how to launch jupyter notebook from cmd 
Python :: how to get chat first name in telebot 
Python :: exact distance 
Python :: send email with python 
Python :: import static in django urls 
Python :: latest django version 
Python :: splitting a string and appending each character to a list python 
Python :: goal parser 
Python :: Removing all non-numeric characters from string in Python 
Python :: python boxplot legend 
Python :: program to find even numbers in python 
Python :: python how to return max num index 
Python :: drop rows with certain values pandas 
Python :: how to wait until pressing button in tkinter 
Python :: python convert html to text 
Python :: python string exclude non alphabetical characters 
Python :: email authentication python 
Python :: create spark dataframe in python 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =