[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>1 问题
现在有这样一个需求,在一份记录着微信聊天记录的 csv 文件中,去除同一天同一个人发的相同消息的记录。
<>2 实现代码
# -*- coding: utf-8 -*- import pandas as pd import os from tqdm import * class
DeleteRepetition: def __init__(self, finished_filedir='newdata'): '''
初始化数据清洗后的csv文件保存地址 :param finished_filedir: 保存路径 ''' self.finished_filedir =
finished_filedir def deleteCsvRepetition(self, filename): '''
将单个csv文件以ID,时间,消息为主键,去除同天同的人发的相同的话 :param filename:文件名 :return:null ''' #
读取csv文件 try: csvfile = pd.read_csv(filename, encoding="utf-8") # print(csvfile)
except Exception: raise Exception('file is not found!') # 第一行没有数据,去除 csvfile =
csvfile[1:] # print(csvfile) # 复制一份时间用于还原 time = csvfile['时间'].copy() #
print(time) # print(time.str.split(' ', expand=True)[0]) # 按照天为单位,提取时间
csvfile['时间'] = csvfile['时间'].str.split(' ', expand=True)[0] #
print(csvfile['时间']) # 去除同天同人同消息的数据 csvfile.drop_duplicates(subset=['ID', '时间',
'消息'], keep='first', inplace=True) # 恢复时间格式 csvfile['时间'] = time #
print(csvfile) # 写入csv文件 csvfile.to_csv(self.finished_filedir + '/' + filename,
index=0) def deleteCsvsRepetition(self, filedir): ''' 将根目录下的所有文件进行去重处理 :param
filedir: 文件夹名 ''' # 读取根目录下所有的csv文件名 filenames_csv = os.listdir(filedir) for
filename in tqdm(filenames_csv): self.deleteCsvRepetition(filedir + '/' +
filename) if __name__ == '__main__': test = DeleteRepetition()
test.deleteCsvsRepetition('20190311_0408')