Text deduplication tool and text deduplication Tool

Source: Internet
Author: User

Text deduplication tool and text deduplication Tool

#!/usr/bin/env python# -*- coding: utf-8 -*-# Author: Forsakenimport getoptimport osimport sysdef main():    try:        opts, args = getopt.getopt(sys.argv[1:], 'hf:s', ['help', 'file=', 'sort'])    except getopt.GetoptError as e:        print('[-] %s' % e)        usage()        sys.exit(2)    file = ''    sort = False    for o, a in opts:        if o in ('-h', '--help'):            usage()            sys.exit()        elif o in ('-f', '--file'):            file = a        elif o in ('-s', '--sort'):            sort = True        else:            pass    if not file:        print('[-] File Arguments Not Found!')        usage()        sys.exit(2)    if not os.path.exists(file):        print('[-] File Not Found!')        sys.exit(1)    with open(file, 'r') as f:        old = f.readlines()    old_len = len(old)    new = list()    for o in old:        if not o in new:            new.append(o)    new_len = len(new)    delete = old_len - new_len    if sort:        new.sort()    out = 'new_' + file    with open(out, 'w') as f:        f.writelines(new)    print('Delete %s Line' % delete)    print('Please Check %s' % out)def usage():    print('Usage: python %s [options]' % sys.argv[0])    print('')    print('Options:')    print('  -h, --help                        Show Help Message And Exit')    print('  -f FILE, --file=FILE              File')    print('  -s, --sort                        Sort')if __name__ == '__main__':    main()

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.