当前位置:编程学习 > python >>

python对txt文档内容去除重复行

使用Python编写的脚本,该脚本能够读取文本文件并去除其中的重复行,最后将结果保存到新的文件中。此方法对于处理大量文本数据时去除重复项非常有效。

import shutil
readPath='cnews.test1.txt'
writePath='cnews.test2.txt'
lines_seen=set()
outfiile=open(writePath,'a+',encoding='utf-8')
f=open(readPath,'r',encoding='utf-8')
for line in f:
    if line not in lines_seen:
        outfiile.write(line)
        lines_seen.add(line)


CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,