Python 3.2 数据处理实战:文件读写与规则过滤详解
本文基于Python 3.2版本,详细解析了数据处理小程序的开发过程。内容涵盖文件打开模式(如r+, w+, rb等)、流程控制、for循环、集合与字符串判断、逻辑运算以及正则过滤和字符串处理技巧。通过具体代码示例,展示了如何读取特定文件、应用规则过滤并写入结果,适合初学者参考Python基础语法在实际场景中的应用。
Python 处理数据的实例详解
近期使用 Python 3.2 版本编写了一个依据特定规则处理数据的小型程序,其中涉及了多项 Python 常用基础知识,现对此进行总结。
主要涉及的技术点包括:1. Python 读取文件;2. Python 写入文件;3. Python 的流程控制;4. Python 的 for 循环;5. 在 Python 集合或字符串中判断元素是否存在;6. Python 的逻辑或(or)与逻辑与(and)运算;7. Python 的正则表达式过滤;8. Python 字符串的空格忽略、前缀判断以及按指定字符拆分为列表。
Python 中打开文件的各种模式如下:
关于 open 函数的模式参数说明:
w 表示以写入方式打开文件; a 表示以追加模式打开(从文件末尾开始,必要时创建新文件); 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 数据处理实例的详细介绍。如有疑问,欢迎留言或在本站社区交流讨论。感谢阅读,希望本文能对大家有所帮助,谢谢大家对本站的支持!
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









