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
投稿

猜你喜欢

  • 浅析“Rich”设计模式

    2009-03-12 12:36:00
  • Python中最常用的操作列表的几种方法归纳

    2021-01-11 22:06:05
  • python实现12306抢票及自动邮件发送提醒付款功能

    2021-03-12 19:01:46
  • Python sklearn对文本数据进行特征化提取

    2023-05-19 09:07:04
  • python实现zencart产品数据导入到magento(python导入数据)

    2022-06-04 12:34:32
  • python使用参数对嵌套字典进行取值的方法

    2022-04-12 10:13:27
  • python3实现名片管理系统

    2022-01-21 22:11:01
  • CentOS 6.6服务器编译安装lnmp(Nginx1.6.2+MySQL5.6.21+PHP5.6.3)

    2023-11-15 06:40:50
  • Python定义函数实现累计求和操作

    2021-07-07 00:54:19
  • 对Python 除法负数取商的取整方式详解

    2023-10-02 20:21:41
  • 剖析SQL Server 事务日志的收缩和截断

    2009-01-15 13:04:00
  • Python3将数据保存为txt文件的方法

    2023-01-22 19:06:27
  • 深入了解Golang中Slice切片的使用

    2023-09-25 08:59:08
  • Python如何实现大型数组运算(使用NumPy)

    2023-05-11 21:49:05
  • JSON.stringify转换JSON时日期时间不准确的解决方法

    2014-07-20 13:25:07
  • ORACLE常见错误代码的分析与解决(一)

    2010-08-02 13:20:00
  • Python编程中的异常处理教程

    2022-10-16 04:26:23
  • sqlserver 存储过程动态参数调用实现代码

    2011-10-24 19:41:22
  • 小技巧解决“FF不能读取outerHTML”的问题

    2009-02-10 10:44:00
  • asp中文URL编码server.urlencode

    2008-08-08 11:22:00
  • asp之家 网络编程 m.aspxhome.com