Python 处理数据的实例详解

张开发
2026/4/19 1:31:14 15 分钟阅读

分享文章

Python 处理数据的实例详解
Python 处理数据的实例详解最近用python3.2的版本写了根据特定规则处理数据的一个小程序用到了一些python常用的基础知识在此总结一下1python读文件2python写文件3python的流程控制4python的for循环5python的集合或字符串里判断是否存在某个元素6python的逻辑或逻辑与7python的正则过滤8python的字符串忽略空格和以某个字符串开头和按某个字符拆分成listpython的打开文件的模式关于open 模式w 以写方式打开a 以追加模式打开 (从 EOF 开始, 必要时创建新文件)r 以读写模式打开w 以读写模式打开 (参见 w )a 以读写模式打开 (参见 a )rb 以二进制读模式打开wb 以二进制写模式打开 (参见 w )ab 以二进制追加模式打开 (参见 a )rb 以二进制读写模式打开 (参见 r )wb 以二进制读写模式打开 (参见 w )ab 以二进制读写模式打开 (参见 a )处理代码如下123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112defshowtxt(path,outpathname,detailpath):greenpathrC:\\Users\\qindongliang\\Desktop\\tnstxt\\green.txt;redpathrC:\\Users\\qindongliang\\Desktop\\tnstxt\\red.txt;redsetlisttxt(redpath)greensetlisttxt(greenpath)print(红色词数量: ,len(redset))print(绿色词数量: ,len(greenset))#符合1条件的内容写入f1open(rC:\Users\qindongliang\Desktop\tnstxt\result\\detailpath\\1.txt,encodingUTF-8,modea)#符合2条件的内容写入f2open(rC:\Users\qindongliang\Desktop\tnstxt\result\\detailpath\\2.txt,encodingUTF-8,modea)#符合3条件的内容写入f3open(rC:\Users\qindongliang\Desktop\tnstxt\result\\detailpath\\3.txt,encodingUTF-8,modea)#符合4条件的内容写入f4open(rC:\Users\qindongliang\Desktop\tnstxt\result\\detailpath\\4.txt,encodingUTF-8,modea)delcount1;fopen(path,encodingUTF-8,moder)fnewopen(outpathname,encodingUTF-8,modea)flogopen(outpathname.log,encodingUTF-8,modea)#count1;forlineinf:listline.strip().split(\t)lineline.strip()catalogidlist[0]scorelist[1]keywordclear(list[4].strip())ifkeywordinredset:ifcatalogid.startswith(018022)orcatalogid.startswith(018035)orcatalogid.startswith(014023003) :f1.write(line\n)#符合1条件写入fnew.write(line\n)#符合1条件写入else:flog.write(line 不符合条件1 \n)delcountdelcount1ifkeywordingreenset:ifnot(catalogid.startswith(018022)orcatalogid.startswith(018035)orcatalogid.startswith(014023003)) :fnew.write(line\n)else:f2.write(line\n)flog.write(line 不符合条件2\n)delcountdelcount1flistformatStrList(keyword)ifsexyinflistorsexinflist:ifcatalogid.startswith(018022)orcatalogid.startswith(018035)orcatalogid.startswith(014023003) :f3.write(line\n)fnew.write(line\n)else:flog.write(line 不符合条件3\n)delcountdelcount1#if (keyword.find(underwear)!-1) keyword.find(sexy)-1 keyword.find(sex)-1:ifunderwearinflistandsexynotinflistandsexnotinflist:ifcatalogid.startswith(014032) :f4.write(line\n)fnew.write(line\n)else:flog.write(line 不符合条件4\n)delcountdelcount1#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()importredefclear(str):strre.sub([\\\\],,str)returnstrdefformatStrList(keyword):listkeyword.split( )foriteminlist:item.strip();returnlistdeflisttxt(path):fopen(path,encodingUTF-8)sset()forlineinf:s.add(line.strip())f.close()returnspath1rC:\\Users\\qindongliang\\Desktop\\tnstxt\\highfrequency.txtpathout1rC:\\Users\\qindongliang\\Desktop\\tnstxt\\detail\\a_highfrequency.txtdetail1pathhighfrequencypath2rC:\\Users\\qindongliang\\Desktop\\tnstxt\\highfrequency_d1.txtpathout2rC:\\Users\\qindongliang\\Desktop\\tnstxt\\detail\\b_highfrequency_d1.txtdetail2pathhighfrequency_d1#showtxt(path1,pathout1,detail1path)showtxt(path2,pathout2,detail2path)以上就是对Python 的数据处理的实例详解

更多文章