Example used by the Pythonfileinput module

Source: Internet
Author: User
Tags glob
This article mainly introduces the use cases of the Pythonfileinput module. This article describes the typical usage, basic format, default format, common functions, and common examples, you can refer to the fileinput module to perform operations such as iteration and traversal on the content of one or more files.
The input () function of this module is somewhat similar to the file readlines () method, the difference is:
The former is an iteration object, that is, only one row is generated at a time, and the for loop iteration is required.
The latter reads all rows at a time. When reading large files, the former is undoubtedly more efficient.
It is very convenient to use fileinput to traverse the file cyclically, format the output, search for and replace the file.

[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: specifies the sequence of the Upload file. multiple Upload files include '1.txt', '2.txt ',...].
Inplace: # whether to write the standard output results back to the file, which is not replaced by default
Backup: # specifies the extension of the backup file, such as. bak. If the backup file of this file already exists, it will be overwritten automatically.
Bufsize: # buffer size. the default value is 0. if the file size is large, you can modify this parameter. Generally, the default value is 0.
Mode: # read/write mode. the default value is read-only.
Openhook: # This hook is used to control all opened files, such as encoding methods;


[Common functions]

The code is as follows:


Fileinput. input () # returns objects that can be used for loop traversal.
Fileinput. filename () # returns the name of the current file.
Fileinput. lineno () # returns the number (or serial number) of rows currently read)
Fileinput. filelineno () # returns the row number of the currently read row.
Fileinput. isfirstline () # check whether the current row is the first row of the file
Fileinput. isstdin () # judge whether the last row is read from stdin
Fileinput. close () # close the queue


[Common example]

Example 01: use fileinput to read all rows of a file

The code is as follows:


>>> Import fileinput
>>> For line in fileinput.input('data.txt '):
Print line,
# Output result
Python
Java
C/C ++
Shell

Command line method:

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

Example 02: use fileinput to operate multiple files and modify the content in the same 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 method:

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)

# Execute commands
C: \ Python27> python test. py 1.txt 2.txt

Example 03: use fileinput to replace the file content and back up the original file

The code is as follows:


# Sample file:
#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 ') # or print line. replace ('Python', 'Perl '),

# Final result:
#Data.txt
Python
Java
C/C ++
Shell
# And generate:
Upload data.txt. bak file


The code is as follows:


# The effect is equivalent to the following method:
Import fileinput
For line in fileinput. input ():
Print 'tag: ', line,


# --- Test results:
D: \> python Learn. py <data.txt> data_out.txt

Example 04: convert a CRLF file into an LF file using fileinput

The code is as follows:


Import fileinput
Import sys

For line in fileinput. input (inplace = True ):
# Convert a text file in Windows/DOS format to a Linux File
If line [-2:] = "\ r \ n ":
Line = line + "\ n"
Sys. stdout. write (line)

Example 05: Simple file processing 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)

# Output result
>>>
=> The Zen of Python, by Tim Peters
=>
=> Beautiful is better than Uugly.
=> 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 shocould never pass silently.
=> Unless explicitly silenced.
=> In the face of ambiguity, refuse the temptation to guess.
=> There shoshould 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!

Example 06: use fileinput to process files in batches

The code is as follows:


# --- Test File: test.txt test1.txt test2.txt test3.txt ---
# --- Script File: 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 result:
>>>
-------------------- 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: use fileinput and re for log analysis: extract all rows with dates

The code is as follows:


# -- Sample file --
Aaa
13:45:30 Error: *** Due to System Disk spacke not enough...
Bbb
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 result ---
=> 1970-01-01 13:45:30 Error: ***** Due to System Disk spacke not enough...
=> 10:20:30 Error: ***** Due to System Out of Memory...

Example 08: use fileinput and re for analysis: extract qualified phone numbers

The code is as follows:


# --- Sample File: 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}' # extract the area code 010 or 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 result :---
>>>
========================================================== ============
Filename: phone.txt | Line Number: 3 | 010-99999999
========================================================== ============
Filename: phone.txt | Line Number: 5 | 021-88888888
>>>

Example 09: use fileinput to implement functions similar to grep

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
# Search for all py files containing the import re
AddressBook. py 2 import re
AddressBook1.py 10 import re
AddressBook2.py 18 import re
Test. py 238 import re

Example 10: use fileinput for regular expression 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 '\*
(. Timeout)
# (. *) ', R' \ 1', line. rstrip ())
Print (line)

# --- Output result:
C: \ Python27> python test. py input.txt
Learning Python

Example 11: use fileinput for regular expression replacement, and replace different character 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' ^ ([^ &] *) (&) ([^ &] *) (&) ([^ &] *) ')
# The entire line is separated [@! $ First] interchange with [* %-Second]
For line in fileinput.input('test.txt ', inplace = 1, backup ='. bak '):
Print regex. sub (r' \ 3 \ 2 \ 1 \ 4 \ 5', line ),

# --- Output result:
[* %-Second] & [@! $ First] & [Third]

Example 12: use fileinput to replace 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. 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 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 ")):
# Execute the Chinese character set encoding for the opened file
Line = line. rstrip (). replace (source, target)
Print line

# --- Output result:
C: \> python test. py 192.168.100 127.0.0 host.txt
# Convert all 192.168.100 in the host file 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.