程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[python] fileinput module

編輯:Python

fileinput The module can process input from one or more files .
Automatically read command line parameters ( from sys.argv) And treat it as a list of input files --> Read data rows in order

【 example 1】drop2hash.py The function of is to eliminate all with “##” Beginning line

import fileinput
def main():
for line in fileinput.input():
if not line.startswith('##'):
print(line, end="")
main()

Now suppose there are two data files
sole1.tst

## sole1.tst: test data for the sole function
0 0 0
0100 0
##
0100100

sole2.tst

## sole2.tst: more test data for the sole function
12 15 0
##
100100 0

perform  python3 drop2hash.py sole1.tst sole2.tst​​
As a result, the data of these two files will be spliced , And eliminate all comment lines

# python3 drop2hash.py sole1.tst sole2.tst
0 0 0
0100 0
0100100
12 15 0
100100 0

This module also provides many other functions , You can know the total number of rows read at any time (lineno)、 The number of lines read from the current file (filelineno)、 Current filename (filename)、 Whether the current line is the first line of the file (isfirstline)、 Is it reading from standard input (isstdin). You can also jump to the next file at any time (nextfile) Or close the entire file stream (close).

【 example 2】linestatistics.py Splice the text lines in the input file , And add the file start delimiter , At the same time, count the number of rows

import fileinput
def main():
for line in fileinput.input():
if fileinput.isfirstline():
print("<start of file {0}>".format(fileinput.filename()))
print("Progress>>> curent file lines: ",fileinput.filelineno(),
" total lines: ",fileinput.lineno())
main()

python3 linestatistics.py sole1.tst sole2.tst The results are as follows

# python3 linestatistics.py sole1.tst sole2.tst
<start of file sole1.tst>
Progress>>> curent file lines:  1  total lines:  1
Progress>>> curent file lines:  2  total lines:  2
Progress>>> curent file lines:  3  total lines:  3
Progress>>> curent file lines:  4  total lines:  4
Progress>>> curent file lines:  5  total lines:  5
<start of file sole2.tst>
Progress>>> curent file lines:  1  total lines:  6
Progress>>> curent file lines:  2  total lines:  7
Progress>>> curent file lines:  3  total lines:  8
Progress>>> curent file lines:  4  total lines:  9

If the fileinput.input With a file name or file name list as a parameter , These files will be used as input files , Instead of sys.argv Parameters in .fileinput.input There is also an optional parameter inplace, You can save the output results back to the input file , At the same time, keep the original file as a backup file .

【 example 3】addlinenum.py The contents are as follows ,fileinput.input With file name

import fileinput
with fileinput.input(files="sole1.tst",inplace=False) as f:
for line in f:
line = line.strip()
num = fileinput.lineno()
print("#{0} {1}".format(num, line))

perform  python3 addlinenum.py The screen is as follows ,inplace=False Input file sole1.tst The content remains the same

#1 ## sole1.tst: test data for the sole function
#2 0 0 0
#3 0100 0
#4 ##
#5 0100100

take addlinenum.py The content is modified as follows ,inplace=True,backup="solebk", No screen , Modify the source file directly ; Back up the source file to sole1.tstsolebk; If not provided backup Parameters are not backed up

[[email protected] ~]# cat addlinenum.py
import fileinput
with fileinput.input(files="sole1.tst",inplace=True,backup="solebk") as f:
for line in f:
line = line.strip()
num = fileinput.lineno()
print("#{0} {1}".format(num, line))
[[email protected] ~]#
============================================================
[[email protected] ~]# cat sole1.tst
## sole1.tst: test data for the sole function
0 0 0
0100 0
##
0100100
[[email protected] ~]#
[[email protected] ~]# python3 addlinenum.py
[[email protected] ~]#
[[email protected] ~]# ls -l sole1*
-rw-r--r-- 1 root root 85 Jul 3 11:32 sole1.tst
-rw-r--r-- 1 root root 70 Jul 3 11:32 sole1.tstsolebk
[[email protected] ~]#
[[email protected] ~]# cat sole1.tst
#1 ## sole1.tst: test data for the sole function
#2 0 0 0
#3 0100 0
#4 ##
#5 0100100
[[email protected] ~]#
[[email protected] ~]# cat sole1.tstsolebk
## sole1.tst: test data for the sole function
0 0 0
0100 0
##
0100100
[[email protected] ~]#

Reference material :

Python Common standard library fileinput  https://www.cnblogs.com/nykuo/p/13024272.html
《Python Quick start ( The first 3 edition )》11.1.5 fileinput Use of modules


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved