您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下怎么使用python对多个txt文件中的数据进行筛选的方法,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
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文件中的数据进行筛选的方法”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。