[python] fileinput模块

fileinput模块能处理来自一个或多个文件的输入。
自动读取命令行参数(由sys.argv)并将其视为输入文件的列表 --> 按顺序读取数据行

【例1】drop2hash.py的作用是剔除所有以“##”开头的行

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

main()

现假定有两个数据文件
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

执行 python3 drop2hash.py sole1.tst sole2.tst​​
结果将会把这两个文件的数据拼接起来,并剔除所有的注释行

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

该模块还提供了很多其他函数,可随时了解已读取的总行数(lineno)、已从当前文件读取的行数(filelineno)、当前文件名(filename)、当前行是否为文件的首行(isfirstline)、是否正从标准输入读取(isstdin)。还可随时跳到下一个文件(nextfile)或关闭整个文件流(close)。

【例2】linestatistics.py 把输入文件中的文本行拼接起来,并加上文件开始分界符,同时统计行数

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的执行结果如下

# 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

如果调用fileinput.input时带了一个文件名或文件名列表作为参数,这些文件就会被用作输入文件,而不再采用sys.argv中的参数。fileinput.input还有一个可选参数inplace,可将输出结果存回输入文件中,同时将原始文件保留为备份文件。

【例3】addlinenum.py 内容如下,fileinput.input时带文件名

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))

执行 python3 addlinenum.py 打屏如下,inplace=False 输入文件sole1.tst内容不变

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

将addlinenum.py 内容修改如下,inplace=True,backup="solebk",不打屏,直接修改源文件;备份源文件到sole1.tstsolebk;如果不提供backup参数则不备份

[root@k8s-node2 ~]# 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))
[root@k8s-node2 ~]#


============================================================
[root@k8s-node2 ~]# cat sole1.tst
## sole1.tst: test data for the sole function
0 0 0
0100 0
##
0100100
[root@k8s-node2 ~]#
[root@k8s-node2 ~]# python3 addlinenum.py
[root@k8s-node2 ~]#
[root@k8s-node2 ~]# 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
[root@k8s-node2 ~]#
[root@k8s-node2 ~]# cat sole1.tst
#1 ## sole1.tst: test data for the sole function
#2 0 0 0
#3 0100 0
#4 ##
#5 0100100
[root@k8s-node2 ~]#
[root@k8s-node2 ~]# cat sole1.tstsolebk
## sole1.tst: test data for the sole function
0 0 0
0100 0
##
0100100
[root@k8s-node2 ~]#

参考资料:

Python常用标准库之fileinput  https://www.cnblogs.com/nykuo/p/13024272.html
《Python 快速入门(第3版)》11.1.5 fileinput模块的使用

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值