利用python对多个txt文件中的数据进行筛选 |
您所在的位置:网站首页 › wallpaper怎么筛选文件大小 › 利用python对多个txt文件中的数据进行筛选 |
一、问题描述 筛选出多个txt文件中需要的数据 二、数据准备 三、程序编写 import os def eachFile(filepath): pathDir =os.listdir(filepath) #遍历文件夹中的text return pathDir def readfile(name): fopen=open(name,'r') for lines in fopen.readlines(): #按行读取text中的内容 lines = lines.replace("\n", "").split(",") if 'aaa' in str(lines) and '2' not in str(lines): #筛选出含有'aaa'并且不含数字2的每一行 print(lines) fopen.close() filePath = "C:\\Users\\Administrator\\Desktop\\123" pathDir=eachFile(filePath) for allDir in pathDir: # child = os.path.join('%s%s' % (filepath, allDir)) child = "C:\\Users\\Administrator\\Desktop\\123" + '\\' + allDir readfile(child)以上只是利用if条件句对数据进行简单的筛选,可以用正则表达式做更复杂的数据筛选。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |