python读写文件去重、RE、set的使用

 

Python读写文件是去重存储数据,RE、set的使用

 
# -*- coding:utf-8 -*-

from datetime import datetime
import re


def Main():
    sourcr_dir = '/11yue_all.txt'
    target_dir = '/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()

 

 

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

 

 

 

发布了28 篇原创文章 · 获赞 16 · 访问量 7万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览