介绍
本篇文章为大家展示了使用python怎么删除文件中的重复字段,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
原文件内容放在列表中,新文件内容按行查找,如果没有出现在列表中则写入第三个文件中。
import csv 时间=filetxt1 & # 39; E:/gg/日志/log1.txt& # 39; 时间=filecsv1 & # 39; E:/gg/日志/log1.csv& # 39; 时间=filecsv2 & # 39; E:/gg/日志/log2.csv& # 39; 时间=filecsv3 & # 39; E:/gg/日志/log3.csv& # 39; class operFileCsv (): ,def __init__(自我,,文件名=None): self.filename 才能=,文件名 ,def readCsvFile(自我): readCsvHandler =,开放才能(self.filename, & # 39; " # 39;) filelines 才能=,csv.reader (readCsvHandler,方言=& # 39;excel # 39;) for 才能;fileline  filelines:拷贝 ,才能打印(fileline) readCsvHandler.close才能 ,def writeCsvFile(自我,,writeline): writeCsvHandler =,开放才能(self.filename, & # 39;一个# 39;,,换行符=& # 39;& # 39;) csvWrite 才能=,csv.writer (writeCsvHandler,方言=& # 39;excel # 39;,,) csvWrite.writerow才能(writeline) writeCsvHandler.close才能() class getLogBuffFromFile (): 自我,def __init__ (): 时间=self.logBuff1 才能;[] ,def getLog1Buff(自我,,文件名): with 才能打开(文件名),as 文件句柄: ,,while 真正的: ,,,logOneLine =, filehandler.readline () .strip () ,,,if not logOneLine: ,,,,休息 ,,,self.logBuff1.append (logOneLine) #,才能打印(& # 39;跟踪:,,log1 has & # 39;,, len (self.logBuff1), & # 39;,行# 39;公司) return self.logBuff1才能 ,def getLog2Buff(自我,,logOneLine): ,通过 class deleteIterantLog (): 自我,def __init__ (): 时间=self.logBuff1List 才能;没有 时间=self.logBuff2OneLine 才能;没有 ,def deleteProcedure (oldlog,自我,还以为,newlog, createlog): 时间=self.logBuff1List 才能;getLogBuffFromFile () .getLog1Buff (oldlog) self.dealProcedure才能(newlog, createlog) ,def dealProcedure (file1name,自我,还以为;file2name): with 才能;开放(file1name, & # 39; " # 39;), as readCsvHandler: ,,filelines =, csv.reader (readCsvHandler,方言=& # 39;excel # 39;) ,,for fileline  filelines:拷贝 ,,,if fileline [1], not 拷贝self.logBuff1List: ,,,,operFileCsv (file2name) .writeCsvFile (fileline) if __name__ ==, & # 39; __main__ # 39;: ,deleteIterantLog () .deleteProcedure (filetxt1, filecsv2,, filecsv3)
小编再为大家分享一段Python用集合把文本中重复的字去掉的方法:
import 操作系统,系统,datetime import 编解码器 with 开放(& # 39;aaaaa.txt& # 39;,, & # 39; " # 39;), as f:,, #读入文本中的文件=,,l  f.readlines (), #, txt中所有字符串读入数据 ,x=集(l [0]) ,for 小姐:拷贝范围(len (l)): x.update才能(l[我]) ,s=啊? join(列表(x)) ,打印(s) with 开放(& # 39;result.txt& # 39; & # 39; wb # 39;), as f1:, #把结果写到文件结果中 ,b=字节(年代,编码=皍tf-8"), 之前,f1.write (b) >上述内容就是使用python怎么删除文件中的重复字段,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
使用python怎么删除文件中的重复字段