小編給大家分享一下怎么使用python對多個txt文件中的數(shù)據(jù)進行篩選的方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。
一、問題描述
篩選出多個txt文件中需要的數(shù)據(jù)
二、數(shù)據(jù)準備
這是我自己建立的要處理的文件,里面是隨意寫的一些數(shù)字和字母
三、程序編寫
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中的內(nèi)容 lines = lines.replace("\n", "").split(",") if 'aaa' in str(lines) and '2' not in str(lines): #篩選出含有'aaa'并且不含數(shù)字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文件中的數(shù)據(jù)進行篩選的方法”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!