首页 > 编程 > Python > 正文

使用python对多个txt文件中的数据进行筛选的方法

2019-11-25 12:26:35
字体:
来源:转载
供稿:网友

一、问题描述

筛选出多个txt文件中需要的数据

二、数据准备

这是我自己建立的要处理的文件,里面是随意写的一些数字和字母

三、程序编写

import osdef eachFile(filepath):          pathDir =os.listdir(filepath)    #遍历文件夹中的text  return pathDirdef 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条件句对数据进行简单的筛选,可以用正则表达式做更复杂的数据筛选。

这篇使用python对多个txt文件中的数据进行筛选的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表