Python fileinput Module Usage examples

Source: Internet
Author: User
The Fileinput module can iterate, traverse, and so on for the contents of one or more files.
The input () function of the module is somewhat similar to the file ReadLines () method, except that:
The former is an iterative object that generates only one row at a time and requires a for loop iteration.
The latter is a one-time read of all rows. When it comes to reading large files, the former is undoubtedly more efficient.
It is very convenient to use Fileinput to iterate the file, format output, find, replace and so on.

"Typical usage"

The code is as follows:


Import Fileinput
For line in Fileinput.input ():
Process (line)

"Basic Format"

The code is as follows:


Fileinput.input ([files[, inplace[, backup[, bufsize[, mode[, Openhook]] []]


"Default Format"

The code is as follows:


Fileinput.input (Files=none, Inplace=false, backup= ", bufsize=0, mode= ' R ', Openhook=none)
Files: #文件的路径列表, the default is stdin mode, multi-file [' 1.txt ', ' 2.txt ',...]
InPlace: #是否将标准输出的结果写回文件, default does not replace
Backup: #备份文件的扩展名, specify only the extension, such as. Bak. If the file's backup file already exists, it will be overwritten automatically.
BufSize: #缓冲区大小, the default is 0, if the file is large, you can modify this parameter, the general default can be
Mode: #读写模式, default is read-only
Openhook: #该钩子用于控制打开的所有文件, such as the encoding method;


"Common functions"

The code is as follows:


Fileinput.input () #返回能够用于for循环遍历的对象
Fileinput.filename () #返回当前文件的名称
Fileinput.lineno () #返回当前已经读取的行的数量 (or serial number)
Fileinput.filelineno () #返回当前读取的行的行号
Fileinput.isfirstline () #检查当前行是否是文件的第一行
Fileinput.isstdin () #判断最后一行是否从stdin中读取
Fileinput.close () #关闭队列


"Common examples"

Example 01: Using Fileinput to read all rows of a file

The code is as follows:


>>> Import Fileinput
>>> for line in Fileinput.input (' Data.txt '):
Print line,
#输出结果
Python
Java
C + +
Shell

Command line mode:

The code is as follows:


#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 + +
Data.txt | Line Number:4 |: Shell

Example 02: Use Fileinput to manipulate multiple files and modify content in place

The code is as follows:


#test. py
#---sample file---
C:\python27>type 1.txt
First
Second

C:\python27>type 2.txt
Third
Fourth
#---sample file---
Import Fileinput

def process (line):
return Line.rstrip () + ' line '

For line in Fileinput.input ([' 1.txt ', ' 2.txt '],inplace=1):
Print process (line)

#---result output---
C:\python27>type 1.txt
First line
Second line

C:\python27>type 2.txt
Third line
Fourth line
#---result output---

Command line mode:

The code is as follows:


#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

Example 03: Replace the file content with Fileinput and back up the original file

The code is as follows:


#样本文件:
#data. txt
Python
Java
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 + +
Shell
#并生成:
#data. txt.bak file


The code is as follows:


#其效果等同于下面的方式
Import Fileinput
For line in Fileinput.input ():
print ' Tag: ', line,


#---Test results:
D:\>python learn.py < data.txt > Data_out.txt

Example 04: Using Fileinput to convert CRLF files to LF

The code is as follows:


Import Fileinput
Import Sys

For line in Fileinput.input (inplace=true):
#将Windows files converted to Linux from a text file in/dos format
If line[-2:] = = "\ r \ n":
Line = line + "\ n"
Sys.stdout.write (line)

Example 05: Simple processing of files using Fileinput

The code is as follows:


#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 the-do it.
= = Although that is 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.
and If the implementation is easy to explain, it could be a good idea.
= Namespaces is one honking great idea – let's do more than those!

Example 06: Working with Fileinput batch files

The code is as follows:


#---Test file: test.txt test1.txt test2.txt test3.txt---
#---script files: 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 (),


#---OUTPUT results:
>>>
--------------------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

Example 07: Log analysis using Fileinput and Re: Extract all rows with dates

The code is as follows:


#--sample FILE--
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

#---Test script---
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)

#---Test results---
= = 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 ...

Example 08: Using Fileinput and RE for analysis: extracting eligible phone numbers

The code is as follows:


#---Sample files: Phone.txt---
010-110-12345
800-333-1234
010-99999999
05718888888
021-88888888

#---Test script: test.py---
Import re
Import Fileinput

pattern = ' [010|021]-\d{8} ' #提取区号为010或021电话号码, format: 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,

#---OUTPUT results:---
>>>
==================================================
Filename:phone.txt | Line Number:3 | 010-99999999
==================================================
Filename:phone.txt | Line Number:5 | 021-88888888
>>>

Example 09: Using Fileinput to implement grep-like functionality

The code is as follows:


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文件中, with the word import re
addressbook.py 2 Import re
addressbook1.py Import re
addressbook2.py Import re
test.py 238 Import Re

Example 10: Using Fileinput to do regular replacement

The code is as follows:


#---Test sample: Input.txt
* [Learning Python] (#author: Mark Lutz)

#---Test script: test.py
Import Fileinput
Import re

For line in Fileinput.input ():
Line = Re.sub (R ' \*
(. ∗)
# (. *) ', R '

\1

', Line.rstrip ())
Print (line)

#---OUTPUT results:
C:\python27>python test.py Input.txt

Learning Python


Example 11: Using Fileinput to do regular replacement, substitution between different word modules

The code is as follows:


#---Test sample: test.txt
[@! $First]&[*%-second]&[third]

#---Test script: test.py
Import re
Import Fileinput

Regex = Re.compile (R ' ^ ([^&]*) (&) ([^&]*) (&) ([^&]*)]
#整行以 & Segmentation, to achieve [@! $First] and [*%-second] Interchange
For line in Fileinput.input (' Test.txt ', inplace=1,backup= '. Bak '):
Print regex.sub (R ' \3\2\1\4\5 ', line),

#---OUTPUT results:
[*%-second]&[@! $First]&[third]

Example 12: Replace with fileinput based on argv command line input

The code is as follows:


#---Sample data: host.txt
# localhost is used to configure the loopback interface
# when the system is booting. Don't change the 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 script: 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

#---OUTPUT results:
C:\>python test.py 192.168.100 127.0.0 host.txt
#将host文件中, all 192.168.100 converted to: 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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.