您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何對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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。