python如何删除文件中重复的字段
作者:dylink 时间:2021-09-19 15:43:12
本文实例为大家分享了python如何删除文件中重复字段的具体代码,供大家参考,具体内容如下
原文件内容放在list中,新文件内容按行查找,如果没有出现在list中则写入第三个文件中。
import csv
filetxt1 = 'E:/gg/log/log1.txt'
filecsv1 = 'E:/gg/log/log1.csv'
filecsv2 = 'E:/gg/log/log2.csv'
filecsv3 = 'E:/gg/log/log3.csv'
class operFileCsv():
def __init__(self, filename=None):
self.filename = filename
def readCsvFile(self):
readCsvHandler = open(self.filename, 'r')
filelines = csv.reader(readCsvHandler, dialect='excel')
for fileline in filelines:
print(fileline)
readCsvHandler.close
def writeCsvFile(self, writeline):
writeCsvHandler = open(self.filename, 'a', newline='')
csvWrite = csv.writer(writeCsvHandler, dialect='excel', )
csvWrite.writerow(writeline)
writeCsvHandler.close()
class getLogBuffFromFile():
def __init__(self):
self.logBuff1 = []
def getLog1Buff(self, filename):
with open(filename) as filehandler:
while True:
logOneLine = filehandler.readline().strip()
if not logOneLine:
break
self.logBuff1.append(logOneLine)
# print('TRACE: The log1 has ', len(self.logBuff1), ' lines.')
return self.logBuff1
def getLog2Buff(self, logOneLine):
pass
class deleteIterantLog():
def __init__(self):
self.logBuff1List = None
self.logBuff2OneLine = None
def deleteProcedure(self, oldlog, newlog, createlog):
self.logBuff1List = getLogBuffFromFile().getLog1Buff(oldlog)
self.dealProcedure(newlog, createlog)
def dealProcedure(self, file1name, file2name):
with open(file1name, 'r') as readCsvHandler:
filelines = csv.reader(readCsvHandler, dialect='excel')
for fileline in filelines:
if fileline[1] not in self.logBuff1List:
operFileCsv(file2name).writeCsvFile(fileline)
if __name__ == '__main__':
deleteIterantLog().deleteProcedure(filetxt1, filecsv2, filecsv3)
小编再为大家分享一段Python用集合把文本中重复的字去掉的方法:
import os,sys,datetime
import codecs
with open('aaaaa.txt', 'r') as f: #读入文本中的文件
l = f.readlines() # txt中所有字符串读入data
x=set(l[0])
for i in range(1,len(l)):
x.update(l[i])
s="".join(list(x))
print(s)
with open('result.txt','wb') as f1: #把结果写到文件result中
b=bytes(s,encoding="utf-8")
f1.write(b)
更多精彩书单,请点击python编程必备书单
领取干货:零基础入门学习python视频教程
来源:https://blog.csdn.net/duanyl123/article/details/83043489
标签:python,删除,重复字段
0
投稿
猜你喜欢
Python函数默认参数设置的具体方法
2021-03-13 08:19:07
Oracle中sys和system的区别小结
2009-11-10 20:36:00
Django处理文件上传File Uploads的实例
2023-07-18 13:44:23
Python使用re模块实现正则表达式操作指南
2022-03-14 18:11:11
TypeScript中泛型的使用详细讲解
2024-04-17 10:35:23
Python答题卡识别并给出分数的实现代码
2022-04-10 03:52:46
利用django+wechat-python-sdk 创建微信服务器接入的方法
2023-06-04 21:28:55
使用documentElement正确取得当前可见区域的大小
2024-04-18 09:34:06
15款非常有用的前端开发CSS网格(grid system)生成器
2009-07-06 13:02:00
python实现图片九宫格分割的示例
2021-02-08 22:19:38
php基于协程实现异步的方法分析
2023-06-11 10:08:39
asp如何定时执行约定的页面?
2009-11-15 20:17:00
SQL server使用自定义函数以及游标
2011-11-03 17:26:27
如何编写一个基于WEB的文件查询系统?
2009-11-08 18:55:00
基于Google的Python编码规范标准
2023-02-21 12:40:38
python3.0 模拟用户登录,三次错误锁定的实例
2022-07-23 01:35:48
OpenCV-Python实现腐蚀与膨胀的实例
2023-06-05 18:07:07
python3.6生成器yield用法实例分析
2022-11-22 00:07:05
用django设置session过期时间的方法解析
2022-11-16 11:36:06
python统计文本字符串里单词出现频率的方法
2021-11-10 17:38:48