标签:遍历 readline search 打开 turn pass cli python 返回
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:
- inplace:
- backup:
- bufsize:
- mode:
- openhook:
【常用函数】
- fileinput.input()
- fileinput.filename()
- fileinput.lineno()
- fileinput.filelineno()
- fileinput.isfirstline()
- fileinput.isstdin()
- fileinput.close()
【常见例子】
- 例子01: 利用fileinput读取一个文件所有行
- >>> import fileinput
- >>> for line in fileinput.input(‘data.txt‘):
- print line,
- Python
- Java
- C/C++
- Shell
命令行方式:
- 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对多文件操作,并原地修改内容
- 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实现文件内容替换,并将原文件作备份
- Python
- Java
- C/C++
- Shell
-
- import fileinput
-
- for line in fileinput.input(‘data.txt‘,backup=‘.bak‘,inplace=1):
- print line.rstrip().replace(‘Python‘,‘Perl‘)
-
- Python
- Java
- C/C++
- Shell
- 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):
-
- if line[-2:] == "\r\n":
- line = line + "\n"
- sys.stdout.write(line)
- 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!
- 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做分析: 提取符合条件的电话号码
- 010-110-12345
- 800-333-1234
- 010-99999999
- 05718888888
- 021-88888888
-
- import re
- import fileinput
-
- pattern = ‘[010|021]-\d{8}‘
-
- 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
- addressBook.py 2 import re
- addressBook1.py 10 import re
- addressBook2.py 18 import re
- test.py 238 import re
- * [Learning Python](
-
- 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做正则替换,不同字模块之间的替换
- [@!$First]&[*%-Second]&[Third]
-
- import re
- import fileinput
-
- regex = re.compile(r‘^([^&]*)(&)([^&]*)(&)([^&]*)‘)
- 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命令行输入做替换
- 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
-
- 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
- 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中fileinput模块介绍
标签:遍历 readline search 打开 turn pass cli python 返回
原文地址:http://www.cnblogs.com/AngryZe/p/6360073.html