网络编程
位置:首页>> 网络编程>> Python编程>> 使用python读取txt文件的内容,并删除重复的行数方法

使用python读取txt文件的内容,并删除重复的行数方法

作者:猫猫与橙子  发布时间:2022-05-08 19:41:56 

标签:python,txt,删除,重复,行数

注意,本文代码是使用在txt文档上,同时txt文档中的内容每一行代表的是图片的名字。


#coding:utf-8
import shutil
readDir = "原文件绝对路经"
writeDir = "写入文件的绝对路径"
#txtDir = "/home/fuxueping/Desktop/1"
lines_seen = set()
outfile=open(writeDir,"w")
f = open(readDir,"r")
for line in f:
 if line not in lines_seen:
   outfile.write(line)
   lines_seen.add(line)
outfile.close()
print "success"

最终结果在在写入文件内容中,没有重复内容。

来源:https://blog.csdn.net/qq_22764813/article/details/73187473

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com