小编给大家分享一下怎么使用python对多个txt文件中的数据进行筛选的方法,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
python有哪些常用库
python常用的库:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。
一、问题描述
筛选出多个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)
看完了这篇文章,相信你对“怎么使用python对多个txt文件中的数据进行筛选的方法”有了一定的了解,如果想了解更多相关知识,欢迎关注天达云行业资讯频道,感谢各位的阅读!