您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了如何对python读写文件去重,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
如下所示:
# -*- coding:utf-8 -*- from datetime import datetime import re def Main(): sourcr_dir = '/data/u_lx_data/fudan/muying/muying_11yue_all.txt' target_dir = '/data/u_lx_data/fudan/muying/python/uid_regular_get.txt' uset = set() #去重 print("开始。。。。。") print(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) with open(target_dir, 'w+') as f_write: with open(sourcr_dir, 'r') as f_scorce: for line in f_scorce: line = line.strip().split("\t") # 宝宝树 if line[2] == 'babytree.com': uidList = re.findall(r'.*NL=u%02(u\d+)', line[3], re.I) if uidList: # 去重代码 if uidList[0] not in uset: f_write.write(uidList[0] + "\n") uset.add(uidList[0]) print("宝宝树已完成") # 柚宝宝 elif line[2] == 'youzibuy.com': if line[4].find("yunqi.youzibuy.com/tae_top_notify") != -1: uidList = re.findall(r'.*myuid=(\d+)', line[4], re.I) if uidList: if uidList[0] not in uset: f_write.write(uidList[0] + "\n") uset.add(uidList[0]) print("柚宝宝已完成") # 妈妈帮 elif line[2] == 'mmbang.com': uidList = re.findall(r'.*uid=(\d+)', line[3], re.I) if uidList: if uidList[0] not in uset: f_write.write(uidList[0] + "\n") uset.add(uidList[0]) print("妈妈帮已完成") # 妈妈网 elif line[2] == 'mama.cn': if line[4].find("mapi.mama.cn/feed/users/show") != -1: uidList = re.findall(r'.*friend_uid=(\d+)', line[4], re.I) if uidList: if uidList[0] not in uset: f_write.write(uidList[0] + "\n") uset.add(uidList[0]) if line[4].find("mamaquan/mmq_thread") != -1: uidList = re.findall(r'.*uid=(\d+)', line[4], re.I) if uidList: if uidList[0] not in uset: f_write.write(uidList[0] + "\n") uset.add(uidList[0]) print("妈妈网已完成") # 育儿网 elif line[2] == 'ci123.com': uidList = re.findall(r'.*ci123js=([a-zA-Z]+\d+)', line[3], re.I) if uidList: if uidList[0] not in uset: f_write.write(uidList[0] + "\n") uset.add(uidList[0]) print("育儿网已完成") print("完成。。。。。") print(datetime.now().strftime('%Y-%m-%d %H:%M:%S')) if __name__ == "__main__": Main()
感谢你能够认真阅读完这篇文章,希望小编分享的“如何对python读写文件去重”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。