Python 处理数据的实例详解
作者:qindongliang1922 发布时间:2021-05-31 21:29:35
标签:Python,数据处理
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)
来源:http://qindongliang.iteye.com/blog/2162304


猜你喜欢
- 基于python的Appium进行b站直播消费记录爬取之前看文章说fiddler也可以进行爬取,但尝试了一下没成功,这次选择appium进行
- 直接使用==比较的情况分类说明是否能比较说明基本类型整型( int/uint/int8/uint8/int16/uint16/int32/u
- 1、准备数据以下操作将在该表中进行create table student ( id int unsigned primary key au
- 前置准备一个域名一台服务器一个公众号域名配置在你的域名服务商新建二级域名并绑定服务器主机IP服务器配置上传下面的python文件到你的服务器
- 前几天遇到一个问题,需要把网页中的一部分内容挑出来,于是找到了urllib和HTMLParser两个库.urllib可以将网页爬下来,然后交
- 目录什么是 MySQL?什么是 MongoDB?MongoDB 与 MySQL 的差异数据模式和容量性能和速度安全性事务的特性:原子性、一致
- JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它
- 第一种:利用functools 工具处理import functoolsresult = (lambda k: functools.redu
- 本文以抢购、秒杀为例。介绍如何在高并发状况下确保数据正确。 在高并发请求下容易参数两个问题 1.数据出错,导致产品超卖。 2.频繁操作数据库
- 目的测试一个对象是否是字符串方法Python的字符串的基类是basestring,包括了str和unicode类型。一般可以采用以下方法:d
- 标题可能无法表达我的本意。比如,有这样一个枚举:public enum MyChoice { &nb
- 最近碰到一个mysql5数据库的问题。就是一个标准的servlet/tomcat网络应用,后台使用mysql数据库。问题是待机一晚上后,第二
- Django的信号Django的信号机制不同于Linux的信号机制,Django 中的信号用于在框架执行操作时解耦。当某些动作发生的时候,系
- 1、plotly库的相关介绍1)相关说明plotly是一个基于javascript的绘图库,plotly绘图种类丰富,效果美观;易于保存与分
- 最近的工作中涉及到大量的ajax操作,本来该后台做的事也要我来做了.而现在使用的ajax函数是一个后台人员封装的—-但他又是基于jquery
- 1.先引入switchery.css 和 switchery.js2.绘制 checkbox按钮 : a.静态页面写法 未选中时
- 废话不多说,先给大家看下python实现屏幕截图的代码,具体代码如下所述:from selenium import webdriverimp
- 1 、据说python3就没有这个问题了2 、u'字符串' 代表是unicode格式的数据,路径最好写成这个格式,别直接跟字
- SMTPSMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件、HTML邮件以及带附件的邮件。Python对SMTP
- 思路复原魔方困难问题的分解:1、用合适的数据结构表示出三阶魔方的六个面以及每一面的颜色2、每一次不同旋转操作的实现3、复原时如何判断当前魔方