Python 处理数据的实例详解


Posted in Python onAugust 10, 2017

Python 处理数据的实例详解

最近用python(3.2的版本)写了根据特定规则,处理数据的一个小程序,用到了一些python常用的基础知识,在此总结一下:

1,python读文件
2,python写文件
3,python的流程控制
4,python的for循环
5,python的集合,或字符串里判断是否存在某个元素
6,python的逻辑或,逻辑与
7,python的正则过滤
8,python的字符串忽略空格,和以某个字符串开头和按某个字符拆分成list

python的打开文件的模式:

关于open 模式:

w     以写方式打开,
a     以追加模式打开 (从 EOF 开始, 必要时创建新文件)
r+     以读写模式打开
w+     以读写模式打开 (参见 w )
a+     以读写模式打开 (参见 a )
rb     以二进制读模式打开
wb     以二进制写模式打开 (参见 w )
ab     以二进制追加模式打开 (参见 a )
rb+    以二进制读写模式打开 (参见 r+ )
wb+    以二进制读写模式打开 (参见 w+ )
ab+    以二进制读写模式打开 (参见 a+ )

处理代码如下:

def showtxt(path,outpathname,detailpath): 
 
  greenpath=r"C:\\Users\\qindongliang\\Desktop\\tnstxt\\green.txt"; 
  redpath=r"C:\\Users\\qindongliang\\Desktop\\tnstxt\\red.txt"; 
  redset=listtxt(redpath) 
  greenset=listtxt(greenpath) 
  print("红色词数量: ",len(redset)) 
  print("绿色词数量: ",len(greenset)) 
  #符合1条件的内容写入 
  f1=open(r"C:\Users\qindongliang\Desktop\tnstxt\result\\"+detailpath+"\\1.txt",encoding="UTF-8",mode="a+") 
  #符合2条件的内容写入 
  f2=open(r"C:\Users\qindongliang\Desktop\tnstxt\result\\"+detailpath+"\\2.txt",encoding="UTF-8",mode="a+") 
  #符合3条件的内容写入 
  f3=open(r"C:\Users\qindongliang\Desktop\tnstxt\result\\"+detailpath+"\\3.txt",encoding="UTF-8",mode="a+") 
  #符合4条件的内容写入 
  f4=open(r"C:\Users\qindongliang\Desktop\tnstxt\result\\"+detailpath+"\\4.txt",encoding="UTF-8",mode="a+") 
 
 
 
  delcount=1; 
  f=open(path,encoding="UTF-8",mode="r+") 
  fnew=open(outpathname,encoding="UTF-8",mode="a+") 
  flog=open(outpathname+".log",encoding="UTF-8",mode="a+") 
  #count=1; 
  for line in f: 
    list=line.strip().split("\t") 
    line=line.strip() 
    catalogid=list[0] 
    score=list[1] 
    keyword=clear(list[4].strip()) 
    if keyword in redset: 
      if catalogid.startswith("018022") or catalogid.startswith("018035") or catalogid.startswith("014023003") : 
        f1.write(line+"\n")#符合1条件写入 
        fnew.write(line+"\n")#符合1条件写入 
      else: 
        flog.write(line+"  不符合条件1 "+"\n") 
        delcount=delcount+1 
 
    if keyword in greenset: 
      if not (catalogid.startswith("018022") or catalogid.startswith("018035") or catalogid.startswith("014023003")) : 
        fnew.write(line+"\n") 
      else: 
        f2.write(line+"\n") 
        flog.write(line+"  不符合条件2"+"\n") 
        delcount=delcount+1 
 
 
    flist=formatStrList(keyword) 
    if "sexy" in flist or "sex" in flist: 
      if catalogid.startswith("018022") or catalogid.startswith("018035") or catalogid.startswith("014023003") : 
        f3.write(line+"\n") 
        fnew.write(line+"\n") 
      else: 
        flog.write(line+" 不符合条件3"+"\n") 
        delcount=delcount+1 
 
    #if (keyword.find("underwear")!=-1) & keyword.find("sexy")==-1 & keyword.find("sex")==-1: 
    if "underwear" in flist and "sexy" not in flist and "sex" not in flist: 
      if catalogid.startswith("014032") : 
        f4.write(line+"\n") 
        fnew.write(line+"\n") 
      else: 
        flog.write(line+" 不符合条件4"+"\n") 
        delcount=delcount+1 
 
    #print(list[0]," ",list[1]," ",list[4]) 
    #print() 
 
 
 
  flog.write("删除总数目: "+str(delcount)) 
  f.close() 
  f1.close() 
  f2.close() 
  f3.close() 
  f4.close() 
  fnew.close() 
  flog.close() 
 
import re 
def clear(str): 
  str=re.sub("[\"\"\'\'+]","",str) 
  return str 
 
 
def formatStrList(keyword): 
  list=keyword.split(" ") 
  for item in list: 
    item.strip(); 
  return list 
 
 
 
 
def listtxt(path): 
   f=open(path,encoding="UTF-8") 
   s=set() 
   for line in f: 
     s.add(line.strip()) 
   f.close() 
   return s 
 
path1=r"C:\\Users\\qindongliang\\Desktop\\tnstxt\\highfrequency.txt" 
pathout1=r"C:\\Users\\qindongliang\\Desktop\\tnstxt\\detail\\a_highfrequency.txt" 
detail1path="highfrequency" 
path2=r"C:\\Users\\qindongliang\\Desktop\\tnstxt\\highfrequency_d1.txt" 
pathout2=r"C:\\Users\\qindongliang\\Desktop\\tnstxt\\detail\\b_highfrequency_d1.txt" 
detail2path="highfrequency_d1" 
 
#showtxt(path1,pathout1,detail1path) 
 
showtxt(path2,pathout2,detail2path)

以上就是对Python 的数据处理的实例详解,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Python 相关文章推荐
编程语言Python的发展史
Sep 26 Python
python让图片按照exif信息里的创建时间进行排序的方法
Mar 16 Python
Python中几种操作字符串的方法的介绍
Apr 09 Python
Python socket网络编程TCP/IP服务器与客户端通信
Jan 05 Python
Python解决抛小球问题 求小球下落经历的距离之和示例
Feb 01 Python
在pycharm 中添加运行参数的操作方法
Jan 19 Python
python 的numpy库中的mean()函数用法介绍
Mar 03 Python
windows10环境下用anaconda和VScode配置的图文教程
Mar 30 Python
Python通过文本和图片生成词云图
May 21 Python
java关于string最常出现的面试题整理
Jan 18 Python
使用numpy nonzero 找出非0元素
May 14 Python
Python Numpy库的超详细教程
Apr 06 Python
Python 由字符串函数名得到对应的函数(实例讲解)
Aug 10 #Python
关于Python中空格字符串处理的技巧总结
Aug 10 #Python
Python排序算法实例代码
Aug 10 #Python
简单谈谈python中的语句和语法
Aug 10 #Python
Python中如何优雅的合并两个字典(dict)方法示例
Aug 09 #Python
Python中使用多进程来实现并行处理的方法小结
Aug 09 #Python
Python基于matplotlib绘制栈式直方图的方法示例
Aug 09 #Python
You might like
咖啡机如何保养和日常清洁?
2021/03/03 冲泡冲煮
基于thinkphp5框架实现微信小程序支付 退款 订单查询 退款查询操作
2020/08/17 PHP
PHP接入支付宝接口失效流程详解
2020/11/10 PHP
jQuery 数据缓存data(name, value)详解及实现
2010/01/04 Javascript
js复制到剪切板的实例方法
2013/06/28 Javascript
javascript数组去重的六种方法汇总
2015/08/16 Javascript
jQuery实现可移动选项的左右下拉列表示例
2016/12/26 Javascript
jQuery html表格排序插件tablesorter使用方法详解
2017/02/10 Javascript
vuex实现登录状态的存储,未登录状态不允许浏览的方法
2018/03/09 Javascript
对angularjs框架下controller间的传值方法详解
2018/10/08 Javascript
微信小程序实现卡片层叠滑动效果
2019/06/21 Javascript
Vue使用CDN引用项目组件,减少项目体积的步骤
2020/10/30 Javascript
[48:52]DOTA2上海特级锦标赛A组小组赛#2 Secret VS CDEC第一局
2016/02/25 DOTA
pandas 对每一列数据进行标准化的方法
2018/06/09 Python
python3正则提取字符串里的中文实例
2019/01/31 Python
django多个APP的urls设置方法(views重复问题解决)
2019/07/19 Python
一文了解python 3 字符串格式化 F-string 用法
2020/03/04 Python
keras自定义回调函数查看训练的loss和accuracy方式
2020/05/23 Python
python3 kubernetes api的使用示例
2021/01/12 Python
基于DOM+CSS3实现OrgChart组织结构图插件
2016/03/02 HTML / CSS
html5使用canvas画三角形
2014/12/15 HTML / CSS
英国经典球衣网站:Classic Football Shirts
2017/05/20 全球购物
婚礼答谢宴主持词
2014/03/14 职场文书
闭幕式主持词
2014/04/02 职场文书
房产委托公证书样本
2014/04/04 职场文书
吨的认识教学反思
2014/04/27 职场文书
公司门卫岗位职责范本
2014/07/08 职场文书
个人作风建设心得体会
2014/10/22 职场文书
租车协议书
2015/01/27 职场文书
父亲节寄语大全
2015/02/27 职场文书
2015年师德表现自我评价
2015/03/05 职场文书
趣味运动会通讯稿
2015/07/18 职场文书
2015年工商局个人工作总结
2015/07/23 职场文书
手把手教你使用TensorFlow2实现RNN
2021/07/15 Python
什么是Python装饰器?如何定义和使用?
2022/04/11 Python
vue如何在data中引入图片的正确路径
2022/06/05 Vue.js