文本去重(python&shell)

21 篇文章 0 订阅

python:

def TextDR(sourcepath,destpath):
    sum = 0
    sum_pre = 0
    addrs = set()
    with open(sourcepath, 'r',encoding='utf8') as scan_file:
        for line in scan_file.readlines():
            sum_pre += 1
            # addr = get_addr(line)
            # line.decode('utf8')
            addrs.add(line)
    scan_file.close()
    with open(destpath, 'w',encoding='utf8') as infile:
        while len(addrs) > 0:
            sum += 1
            infile.write(addrs.pop())
    infile.close()
    #print(addrs)
    print("去重之前文本条数: "+str(sum_pre))
    print("去重之后文本条数: "+str(sum))
    return sum_pre,sum

#example
sourcepath = r'D:/zz/source.txt'
destpath = r'D:/zz/destpath.txt'
TextDR(sourcepath,destpath)

shell:

#去除重复行(去重)
sort file.txt | uniq -d
#例:
sort t1.txt |uniq -d > newfile1.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值