Python fileinput模块使用实例


Posted in Python onJune 03, 2015

fileinput模块可以对一个或多个文件中的内容进行迭代、遍历等操作。
该模块的input()函数有点类似文件readlines()方法,区别在于:
前者是一个迭代对象,即每次只生成一行,需要用for循环迭代。
后者是一次性读取所有行。在碰到大文件的读取时,前者无疑效率更高效。
用fileinput对文件进行循环遍历,格式化输出,查找、替换等操作,非常方便。

【典型用法】

import fileinput

for line in fileinput.input():

    process(line)

【基本格式】

fileinput.input([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]])

【默认格式】
fileinput.input (files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)

files:                  #文件的路径列表,默认是stdin方式,多文件['1.txt','2.txt',...]  

inplace:                #是否将标准输出的结果写回文件,默认不取代  

backup:                 #备份文件的扩展名,只指定扩展名,如.bak。如果该文件的备份文件已存在,则会自动覆盖。  

bufsize:                #缓冲区大小,默认为0,如果文件很大,可以修改此参数,一般默认即可  

mode:                   #读写模式,默认为只读  

openhook:               #该钩子用于控制打开的所有文件,比如说编码方式等; 

【常用函数】
fileinput.input()       #返回能够用于for循环遍历的对象  

fileinput.filename()    #返回当前文件的名称  

fileinput.lineno()      #返回当前已经读取的行的数量(或者序号)  

fileinput.filelineno()  #返回当前读取的行的行号  

fileinput.isfirstline() #检查当前行是否是文件的第一行  

fileinput.isstdin()     #判断最后一行是否从stdin中读取  

fileinput.close()       #关闭队列 

【常见例子】

例子01: 利用fileinput读取一个文件所有行

>>> import fileinput  

>>> for line in fileinput.input('data.txt'):  

        print line,  

#输出结果  

Python  

Java   

C/C++  

Shell 

命令行方式:

#test.py  

import fileinput  

  

for line in fileinput.input():  

    print fileinput.filename(),'|','Line Number:',fileinput.lineno(),'|: ',line  

  

c:>python test.py data.txt  

data.txt | Line Number: 1 |:  Python  

data.txt | Line Number: 2 |:  Java  

data.txt | Line Number: 3 |:  C/C++  

data.txt | Line Number: 4 |:  Shell 

例子02: 利用fileinput对多文件操作,并原地修改内容

#test.py  

#---样本文件---  

c:\Python27>type 1.txt  

first  

second  

  

c:\Python27>type 2.txt  

third  

fourth  

#---样本文件---  

import fileinput  

  

def process(line):  

    return line.rstrip() + ' line'  

  

for line in fileinput.input(['1.txt','2.txt'],inplace=1):  

    print process(line)  

  

#---结果输出---  

c:\Python27>type 1.txt  

first line  

second line  

  

c:\Python27>type 2.txt  

third line  

fourth line  

#---结果输出--- 

命令行方式:

#test.py  

import fileinput  

  

def process(line):  

    return line.rstrip() + ' line'  

  

for line in fileinput.input(inplace = True):  

    print process(line)  

  

#执行命令  

c:\Python27>python test.py 1.txt 2.txt 

例子03: 利用fileinput实现文件内容替换,并将原文件作备份

#样本文件:  

#data.txt  

Python  

Java  

C/C++  

Shell  

  

#FileName: test.py  

import fileinput  

  

for line in fileinput.input('data.txt',backup='.bak',inplace=1):  

    print line.rstrip().replace('Python','Perl')  #或者print line.replace('Python','Perl'),  

      

#最后结果:  

#data.txt  

Python  

Java  

C/C++  

Shell  

#并生成:  

#data.txt.bak文件 

#其效果等同于下面的方式  

import fileinput  

for line in fileinput.input():  

    print 'Tag:',line,  

  

  

#---测试结果:     

d:\>python Learn.py < data.txt > data_out.txt 

例子04: 利用fileinput将CRLF文件转为LF

import fileinput  

import sys  

  

for line in fileinput.input(inplace=True):  

    #将Windows/DOS格式下的文本文件转为Linux的文件  

    if line[-2:] == "\r\n":    

        line = line + "\n"  

    sys.stdout.write(line) 

例子05: 利用fileinput对文件简单处理

#FileName: test.py  

import sys  

import fileinput  

  

for line in fileinput.input(r'C:\Python27\info.txt'):  

    sys.stdout.write('=> ')  

    sys.stdout.write(line)  

  

#输出结果     

>>>   

=> The Zen of Python, by Tim Peters  

=>   

=> Beautiful is better than ugly.  

=> Explicit is better than implicit.  

=> Simple is better than complex.  

=> Complex is better than complicated.  

=> Flat is better than nested.  

=> Sparse is better than dense.  

=> Readability counts.  

=> Special cases aren't special enough to break the rules.  

=> Although practicality beats purity.  

=> Errors should never pass silently.  

=> Unless explicitly silenced.  

=> In the face of ambiguity, refuse the temptation to guess.  

=> There should be one-- and preferably only one --obvious way to do it.  

=> Although that way may not be obvious at first unless you're Dutch.  

=> Now is better than never.  

=> Although never is often better than *right* now.  

=> If the implementation is hard to explain, it's a bad idea.  

=> If the implementation is easy to explain, it may be a good idea.  

=> Namespaces are one honking great idea -- let's do more of those! 

例子06: 利用fileinput批处理文件

#---测试文件: test.txt test1.txt test2.txt test3.txt---  

#---脚本文件: test.py---  

import fileinput  

import glob  

  

for line in fileinput.input(glob.glob("test*.txt")):  

    if fileinput.isfirstline():  

        print '-'*20, 'Reading %s...' % fileinput.filename(), '-'*20  

    print str(fileinput.lineno()) + ': ' + line.upper(),  

      

      

#---输出结果:  

>>>   

-------------------- Reading test.txt... --------------------  

1: AAAAA  

2: BBBBB  

3: CCCCC  

4: DDDDD  

5: FFFFF  

-------------------- Reading test1.txt... --------------------  

6: FIRST LINE  

7: SECOND LINE  

-------------------- Reading test2.txt... --------------------  

8: THIRD LINE  

9: FOURTH LINE  

-------------------- Reading test3.txt... --------------------  

10: THIS IS LINE 1  

11: THIS IS LINE 2  

12: THIS IS LINE 3  

13: THIS IS LINE 4 

例子07: 利用fileinput及re做日志分析: 提取所有含日期的行

#--样本文件--  

aaa  

1970-01-01 13:45:30  Error: **** Due to System Disk spacke not enough...  

bbb  

1970-01-02 10:20:30  Error: **** Due to System Out of Memory...  

ccc  

  

#---测试脚本---  

import re  

import fileinput  

import sys  

  

pattern = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'  

  

for line in fileinput.input('error.log',backup='.bak',inplace=1):  

    if re.search(pattern,line):  

        sys.stdout.write("=> ")  

        sys.stdout.write(line)  

  

#---测试结果---  

=> 1970-01-01 13:45:30  Error: **** Due to System Disk spacke not enough...  

=> 1970-01-02 10:20:30  Error: **** Due to System Out of Memory... 

例子08: 利用fileinput及re做分析: 提取符合条件的电话号码

#---样本文件: phone.txt---  

010-110-12345  

800-333-1234  

010-99999999  

05718888888  

021-88888888  

  

#---测试脚本: test.py---  

import re  

import fileinput  

  

pattern = '[010|021]-\d{8}'  #提取区号为010或021电话号码,格式:010-12345678  

  

for line in fileinput.input('phone.txt'):  

    if re.search(pattern,line):  

        print '=' * 50  

        print 'Filename:'+ fileinput.filename()+' | Line Number:'+str(fileinput.lineno())+' | '+line,  

  

#---输出结果:---  

>>>   

==================================================  

Filename:phone.txt | Line Number:3 | 010-99999999  

==================================================  

Filename:phone.txt | Line Number:5 | 021-88888888  

>>>  

例子09: 利用fileinput实现类似于grep的功能

import sys  

import re  

import fileinput  

  

pattern= re.compile(sys.argv[1])  

for line in fileinput.input(sys.argv[2]):  

    if pattern.match(line):  

        print fileinput.filename(), fileinput.filelineno(), line  

$ ./test.py import.*re *.py  

#查找所有py文件中,含import re字样的  

addressBook.py  2   import re  

addressBook1.py 10  import re  

addressBook2.py 18  import re  

test.py         238 import re 

例子10: 利用fileinput做正则替换

#---测试样本: input.txt  

* [Learning Python](#author:Mark Lutz)  

      

#---测试脚本: test.py  

import fileinput  

import re  

   

for line in fileinput.input():  

    line = re.sub(r'\* 

(.∗)

#(.*)', r'<h2 id="\2">\1</h2>', line.rstrip())  

    print(line)  

  

#---输出结果:  

c:\Python27>python test.py input.txt  

<h2 id="author:Mark Lutz">Learning Python</h2> 

例子11: 利用fileinput做正则替换,不同字模块之间的替换

#---测试样本:test.txt  

[@!$First]&[*%-Second]&[Third]  

  

#---测试脚本:test.py  

import re  

import fileinput  

  

regex = re.compile(r'^([^&]*)(&)([^&]*)(&)([^&]*)')  

#整行以&分割,要实现[@!$First]与[*%-Second]互换  

for line in fileinput.input('test.txt',inplace=1,backup='.bak'):  

    print regex.sub(r'\3\2\1\4\5',line),  

  

#---输出结果:  

[*%-Second]&[@!$First]&[Third] 

例子12: 利用fileinput根据argv命令行输入做替换

#---样本数据: host.txt  

# localhost is used to configure the loopback interface  

# when the system is booting.  Do not change this entry.  

127.0.0.1      localhost  

192.168.100.2  www.test2.com  

192.168.100.3  www.test3.com  

192.168.100.4  www.test4.com  

  

#---测试脚本: test.py  

import sys  

import fileinput  

  

source = sys.argv[1]  

target = sys.argv[2]  

files  = sys.argv[3:]  

  

for line in fileinput.input(files,backup='.bak',openhook=fileinput.hook_encoded("gb2312")):  

    #对打开的文件执行中文字符集编码  

    line = line.rstrip().replace(source,target)  

    print line  

      

#---输出结果:      

c:\>python test.py 192.168.100 127.0.0 host.txt  

#将host文件中,所有192.168.100转换为:127.0.0  

127.0.0.1  localhost  

127.0.0.2  www.test2.com  

127.0.0.3  www.test3.com  

127.0.0.4  www.test4.com 
Python 相关文章推荐
Python程序设计入门(5)类的使用简介
Jun 16 Python
Python中使用Tkinter模块创建GUI程序实例
Jan 14 Python
python更改已存在excel文件的方法
May 03 Python
让Django支持Sql Server作后端数据库的方法
May 29 Python
django开发post接口简单案例,获取参数值的方法
Dec 11 Python
python mac下安装虚拟环境的图文教程
Apr 12 Python
Python中的 sort 和 sorted的用法与区别
Aug 10 Python
Python for i in range ()用法详解
Sep 18 Python
python装饰器实现对异常代码出现进行自动监控的实现方法
Sep 15 Python
使用python画出逻辑斯蒂映射(logistic map)中的分叉图案例
Dec 11 Python
python+selenium小米商城红米K40手机自动抢购的示例代码
Mar 24 Python
Python实现归一化算法详情
Mar 18 Python
以windows service方式运行Python程序的方法
Jun 03 #Python
自己编程中遇到的Python错误和解决方法汇总整理
Jun 03 #Python
python中list常用操作实例详解
Jun 03 #Python
python中argparse模块用法实例详解
Jun 03 #Python
Python中的推导式使用详解
Jun 03 #Python
对于Python装饰器使用的一些建议
Jun 03 #Python
Python模块搜索概念介绍及模块安装方法介绍
Jun 03 #Python
You might like
PHP将进程作为守护进程的方法
2015/03/19 PHP
PHP开发中csrf攻击的简单演示和防范
2017/05/07 PHP
PHP实现基于栈的后缀表达式求值功能
2017/11/10 PHP
浅析PHP7的多进程及实例源码
2019/04/14 PHP
零基础php编程好学吗
2019/10/11 PHP
js 获取服务器控件值的代码
2010/03/05 Javascript
JQuery从头学起第三讲
2010/07/06 Javascript
Jquery+CSS3实现一款简洁大气带滑动效果的弹出层
2013/05/15 Javascript
javascript判断两个IP地址是否在同一个网段的实现思路
2013/12/13 Javascript
javascript实现带下拉子菜单的导航菜单效果
2015/05/14 Javascript
javascript中类的定义方式详解(四种方式)
2015/12/22 Javascript
学习JavaScript设计模式之迭代器模式
2016/01/19 Javascript
jQuery无刷新上传之uploadify3.1简单使用
2016/06/18 Javascript
原生js实现选项卡功能
2017/03/08 Javascript
Bootstrap Table 在指定列中添加下拉框控件并获取所选值
2017/07/31 Javascript
微信小程序 获取session_key和openid的实例
2017/08/17 Javascript
js构建二叉树进行数值数组的去重与优化详解
2018/03/26 Javascript
vue.js实现的绑定class操作示例
2018/07/06 Javascript
详解基于 Node.js 的轻量级云函数功能实现
2019/07/08 Javascript
vue-amap根据地址回显地图并mark的操作
2020/11/03 Javascript
[02:37]2018DOTA2亚洲邀请赛赛前采访 VP.no[o]ne心中最强SOLO是谁
2018/04/04 DOTA
Java Web开发过程中登陆模块的验证码的实现方式总结
2016/05/25 Python
python中正则的使用指南
2016/12/04 Python
Python3.4实现远程控制电脑开关机
2018/02/22 Python
Python实现去除列表中重复元素的方法小结【4种方法】
2018/04/27 Python
对pyqt5之menu和action的使用详解
2019/06/20 Python
Python爬虫:url中带字典列表参数的编码转换方法
2019/08/21 Python
python银行系统实现源码
2019/10/25 Python
Selenium使用Chrome模拟手机浏览器方法解析
2020/04/10 Python
德国旅游网站:weg.de
2018/06/03 全球购物
护士辞职信模板
2014/01/20 职场文书
简历里的自我评价范文
2014/02/24 职场文书
财务情况说明书范文
2014/05/06 职场文书
2015年话务员工作总结
2015/04/29 职场文书
婚庆答谢词大全
2015/09/29 职场文书
Django操作cookie的实现
2021/05/26 Python