京公网安备 11010802034615号
经营许可证编号:京B2-20210330
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)
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
为什么学习数据分析? 当下,我们已然步入数据要素价值全面释放的智能时代。数据不再只是零散的数字记录,更是驱动新质生产力运 ...
2026-07-24【核心关键词】客户、数据分析、指标体系、数据采集、数据指标、业务数据、分析思路、业务需求、分析方法 【专访摘要】本次 CDA ...
2026-07-24在数据分析、业务建模与数字化运营体系中,原始业务数据普遍存在缺失、重复、异常、口径不一致等质量问题,直接用于分析与建模会 ...
2026-07-24 很多数据分析师能熟练计算均值、标准差,但当被问到“如何用一张图让业务方3秒内看懂核心结论”“面对不同数据类型该怎么选 ...
2026-07-24在数据驱动的精细化运营体系中,指标是业务判断、效果复盘、策略优化的核心依据。随着企业数据化程度提升,指标数量持续膨胀,但 ...
2026-07-23在用户运营与产品增长体系中,留存是衡量产品真实价值与用户粘性的核心标尺,也是决定用户生命周期价值、获客投产比的底层因素。 ...
2026-07-23 很多数据分析师精通Excel、SQL、Python等工具,但当被问到“面对一个具体的业务问题,该用什么分析方法”“描述性分析和诊断 ...
2026-07-23【核心关键词】埋点、产品、互联网、数据库、决策、数据分析、产品经理、商业模式、移动互联网、指标体系、运营模块、大数据平 ...
2026-07-22在高并发、大数据量的业务系统中,单表数据量达到千万级甚至亿级后,会出现查询性能骤降、索引维护成本飙升、存储扩容困难等问题 ...
2026-07-22 很多企业团队并非缺乏指标,而是陷入“指标失控”:仪表盘上堆满实时跳动的数据,却无法回答“当前瓶颈在哪、下一步该做什么 ...
2026-07-22在金融风控、企业运营、行业研究等数据分析场景中,大量数据以面板数据形态存在:例如多家分支机构连续多个季度的风险指标、多位 ...
2026-07-21 很多数据分析师每天都在计算指标、制作报表,但当被问到“什么叫指标数据元”“指标数据标准包含哪些核心维度”“指标数据质 ...
2026-07-21一、活动介绍 2026暑期CDA备考冲刺季,为想利用假期拿证的你量身打造。考点胶囊内容搭配多重硬核福利,让你在旅行、实习、居家 ...
2026-07-21金融行业的运营风险贯穿业务全流程,涵盖交易欺诈、操作违规、流程漏洞、合规偏差、客户信用异常等多元场景,是银行、保险、证券 ...
2026-07-17财产保险作为金融行业的核心板块,涵盖车险、家财险、责任险、企财险等多元品类,是个人与企业抵御财产风险、经营风险的重要保障 ...
2026-07-17 很多数据分析师能熟练写SQL、做透视表,但当被问到“数据是从哪里来的?经过哪些加工才进入数据仓库?ETL具体做了什么?”时 ...
2026-07-17【核心关键词】模块、餐饮、客户、门店、企业、订单、供应链、多样化、产品、生产计划、数据分析、生产管理、物料管理、业务分 ...
2026-07-16在数字化分析时代,原始数据本身不具备业务价值,只有通过科学的统计学方法加工、拆解、验证与解读,才能挖掘数据背后的规律、差 ...
2026-07-16 很多数据分析师能熟练地写SQL、做透视表、算描述性统计,但当被问到“如何预测用户流失概率”“如何归因销量下滑的关键因素 ...
2026-07-16在描述性统计分析、数据预处理、异常值排查与多组数据分布对比工作中,箱线图(Box Plot)是应用最广泛的可视化与统计工具之一。 ...
2026-07-15