提取海量文本文件中含有指定字符串的行
来源:互联网 发布:sql注入测试网站 编辑:程序博客网 时间:2024/05/29 07:47
在工作中遇到一个提取一个目录中所有文本文件中含有特定字符串的行,并将其写入一个文件,于是有了以下代码。
# -*- coding: utf8 -*-import osimport sysimport globimport timereload(sys)sys.setdefaultencoding('utf-8')abspath = os.path.dirname(__file__)sys.path.append(abspath)if abspath=='': os.chdir(sys.path[0]) else: os.chdir(abspath) search_str='你要提取的字符串' #定义要在文本文件提取的字符串if __name__=='__main__': stime=time.time() fetch_list=[] All_in_File=glob.glob(r'*.txt') if len(All_in_File)>0: for infile in All_in_File: read_file_list=open(infile,'r').readlines() one_fetch_list=[j for j in read_file_list if j.find(search_str)>0] if len(one_fetch_list): file_name=infile[0:-4] fetch_list.append(file_name.center(70,'*')+'\n') print u'从文件\t%s\t中提取到\t%d\t条含有\t%s\t的记录'%(infile,len(one_fetch_list),search_str) fetch_list.extend(one_fetch_list) #for t in one_fetch_list: # fetch_list.append(t) fetch_list.append('-'*70+'\n\n') out_txt=open('Fetch_result.txt','w') out_txt.writelines(fetch_list) out_txt.close() print 'coast time:%.2f'%(time.time()-stime,)
0 0
- 提取海量文本文件中含有指定字符串的行
- 去掉文本文件中含有某些字符串的行
- 从文本文件提取数据到数据库指定的字段中
- 用shell从文件中提取指定字符串的行
- 从指定的字符串中提取Email
- 从指定的字符串中提取Email
- 搜索当前数据库中含有指定字符串的表列
- 搜索jar包中含有指定字符串的文件
- 杀死进程名中含有指定字符串的进程
- 在一个10M的文本文件中搜索指定字符串
- Python中提取指定字符串
- 字符串中是否含有指定字母
- 提取指定范围的字符串
- 从文本文件里抽取包含指定字符串的行
- 查找含有指定字符串的文件
- Linux 查找含有指定字符串的文件
- python: 统计 含有指定字符串 的 元素
- 文本文件以指定的字符串分割
- Java生成和解析XML格式文件和字符串
- My New Life!
- 地图开发--逆地理编码regeocoding
- iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
- iOS的基本的设计模式
- 提取海量文本文件中含有指定字符串的行
- debian 设置开机自启动
- 黑马程序员--基础--第一篇--RandomAccessFile,PipedInputStream和ByteArrayInputStream类
- 双网卡双网关冲突问题
- linux 下node.js 使用child_process模块的exec方法所遇问题
- 用于词义消岐的Lesk算法
- hdu 1160 FatMouse's Speed 题解(简单dp)
- 解释器模式(Interpreter Pattern)
- 最近在写个自动下载数据的软件,发现QT的QFtp不大好用