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


Posted in Python onDecember 11, 2018

如下所示:

# -*- 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读写文件去重、RE、set的使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
浅析使用Python操作文件
Jul 31 Python
python实现连续图文识别
Dec 18 Python
python进程和线程用法知识点总结
May 28 Python
Python+opencv 实现图片文字的分割的方法示例
Jul 04 Python
PyQt Qt Designer工具的布局管理详解
Aug 07 Python
django重新生成数据库中的某张表方法
Aug 28 Python
python用quad、dblquad实现一维二维积分的实例详解
Nov 20 Python
python进程的状态、创建及使用方法详解
Dec 06 Python
PyCharm Anaconda配置PyQt5开发环境及创建项目的教程详解
Mar 24 Python
python使用建议技巧分享(三)
Aug 18 Python
手把手教你将Flask应用封装成Docker服务的实现
Aug 19 Python
如何通过python计算圆周率PI
Nov 11 Python
python版本五子棋的实现代码
Dec 11 #Python
python提取具有某种特定字符串的行数据方法
Dec 11 #Python
Python面向对象基础入门之编码细节与注意事项
Dec 11 #Python
Python面向对象基础入门之设置对象属性
Dec 11 #Python
python提取包含关键字的整行数据方法
Dec 11 #Python
django开发post接口简单案例,获取参数值的方法
Dec 11 #Python
python面向对象入门教程之从代码复用开始(一)
Dec 11 #Python
You might like
PHP操作文件的一些基本函数使用示例
2014/11/18 PHP
php curl 获取https请求的2种方法
2015/04/27 PHP
Discuz!X中SESSION机制实例详解
2015/09/23 PHP
利用php抓取蜘蛛爬虫痕迹的示例代码
2016/09/30 PHP
php-7.3.6 编译安装过程
2020/02/11 PHP
JavaScript 面向对象与原型
2015/04/10 Javascript
jquery简单倒计时实现方法
2015/12/18 Javascript
jquery mobile界面数据刷新的实现方法
2016/05/28 Javascript
jQuery ajax MD5实现用户注册即时验证功能
2016/10/11 Javascript
BootStrap的select2既可以查询又可以输入的实现代码
2017/02/17 Javascript
react-native之ART绘图方法详解
2017/08/08 Javascript
浅谈js中的this问题
2017/08/31 Javascript
nodejs搭建本地服务器轻松解决跨域问题
2018/03/21 NodeJs
element-ui table行点击获取行索引(index)并利用索引更换行顺序
2020/02/27 Javascript
JS常见错误(Error)及处理方案详解
2020/07/02 Javascript
[01:03]悬念揭晓 11月26日DOTA2完美盛典不见不散
2017/11/23 DOTA
Python中实现参数类型检查的简单方法
2015/04/21 Python
python用10行代码实现对黄色图片的检测功能
2015/08/10 Python
python实现FTP服务器服务的方法
2017/04/11 Python
浅谈python函数调用返回两个或多个变量的方法
2019/01/23 Python
Python split() 函数拆分字符串将字符串转化为列的方法
2019/07/16 Python
在Python中利用pickle保存变量的实例
2019/12/30 Python
在pycharm中创建django项目的示例代码
2020/05/28 Python
基于python实现音乐播放器代码实例
2020/07/01 Python
Python下划线5种含义代码实例解析
2020/07/10 Python
Canon佳能美国官方商店:购买数码相机、数码单反相机、镜头和打印机
2016/11/15 全球购物
美国体育用品商店:Rally House(NCAA、NFL、MLB、NBA、NHL和MLS)
2018/01/03 全球购物
Python如何实现单例模式
2016/06/03 面试题
课程设计心得体会
2013/12/28 职场文书
中等生评语大全
2014/05/04 职场文书
四风问题自查自纠工作情况报告
2014/10/28 职场文书
2015年父亲节活动总结
2015/02/12 职场文书
村官个人总结范文
2015/03/03 职场文书
二手手机买卖合同范本(2019年版)
2019/10/28 职场文书
mysql知识点整理
2021/04/05 MySQL
在python中读取和写入CSV文件详情
2022/06/28 Python