I tried "a program that removes duplicate statements in Python"

Original story: Program to remove duplicate statements in Python

How about this? I thought it would be easier to write the code if I wanted to comment.

check.py


# -*- coding: utf-8 -*-
import sys
from hashlib import sha256
from os.path import isfile


def output_unique_text(path, enc='utf-8'):
    d = {}
    with open(path, 'rb') as reader, open('output.txt', 'wb') as writer:
        for i, line in enumerate(reader, 1):
            value = sha256(line).hexdigest()
            if d.get(value):
                msg = '{}: "{}" is duplicate.\n'
                decoded = line.decode(enc).strip()
                sys.stdout.buffer.write(bytes(msg.format(i, decoded), enc))
            else:
                d[value] = line
                writer.write(line)


def main():
    if len(sys.argv) <= 1 or not isfile(sys.argv[1]):
        print('specify path/to/filename')
        sys.exit(1)
    output_unique_text(sys.argv[1])
    print('confirm output.txt')


if __name__ == '__main__':
    main()

Recommended Posts

I tried "a program that removes duplicate statements in Python"
A program that removes duplicate statements in Python
I made a payroll program in Python!
A memo that I wrote a quicksort in Python
I tried playing a typing game in Python
[Memo] I tried a pivot table in Python
I tried adding a Python3 module in C
I made a Caesar cryptographic program in Python.
I tried to develop a Formatter that outputs Python logs in JSON
I made a prime number generation program in Python
I tried to implement a pseudo pachislot in Python
I made a prime number generation program in Python 2
I tried to create a class that can easily serialize Json in Python
[Beginner] What happens if I write a program that runs in php in Python?
A Python program in "A book that gently teaches difficult programming"
A general-purpose program that formats Linux command strings in python
I tried to implement a one-dimensional cellular automaton in Python
I tried "How to get a method decorated in Python"
I created a class in Python and tried duck typing
I tried to make a stopwatch using tkinter in python
I tried Line notification in Python
When writing a program in Python
I made a program to collect images in tweets that I liked on twitter with Python
Movement that changes direction in the coordinate system I tried Python 3
I made a program that solves the spot the difference in seconds
[Python] A memo that I tried to get started with asyncio
I tried to implement a misunderstood prisoner's dilemma game in Python
A program that determines whether a number entered in Python is a prime number
I tried to implement PLSA in Python
[Python] A program that creates stairs with #
I tried to implement PLSA in Python 2
Write a Caesar cipher program in Python
I tried to implement ADALINE in Python
I tried a functional language with Python
I tried to implement PPO in Python
A program that plays rock-paper-scissors using Python
I created a password tool in Python.
[Python] A program that rounds the score
[Python] I tried to make a simple program that works on the command line using argparse.
A story that didn't work when I tried to log in with the Python requests module
I made a web application in Python that converts Markdown to HTML
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I made a Discord bot in Python that translates when it reacts
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
I made a program to check the size of a file in Python
I tried to implement a card game of playing cards in Python
I want to create a window in Python
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python
I wrote a class in Python3 and Java
A simple Pub / Sub program note in Python
I tried to implement TOPIC MODEL in Python
I tried reading a CSV file using Python
I tried non-blocking I / O Eventlet behavior in Python
I tried running alembic, a Python migration tool
I tried to implement selection sort in python
I want to create a priority queue that can be updated in Python (2.7)
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
I made a familiar function that can be used in statistics with Python
I want to exe and distribute a program that resizes images Python3 + pyinstaller
[Python] I tried to make a Shiritori AI that enhances vocabulary through battles