Python之fileinput

2024-04-10 03:38
文章标签 python fileinput

本文主要是介绍Python之fileinput,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

fileinput module
fileinput module可以对一个或多个文件中的内容进行迭代、遍历等操作。该模块的input()函数有点类似文件readlines()方法。
区别在于前者是一个迭代对象(iterable object),需要用for循环迭代,后者是一次性读取所有行。

用fileinput对文件进行循环遍历,格式化输出,查找、替换等操作,非常方便。

1. 典型用法

import fileinput
for line in fileinput.input('D:\\HeadFirstPython\\chapter1\\data.txt'):print(line, end = "")
似乎比 with open(filename) as f: for i in f: print(i) 这种要好一些

2. FUNCTIONS

close()
    Close the sequence.
filelineno()
    Return the line number in the current file. Before the first line
    has been read, returns 0. After the last line of the last file has
    been read, returns the line number of that line within the file.
返回当前读取的文件内容的行数
filename()
    Return the name of the file currently being read.
    Before the first line has been read, returns None.
input(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)
    Return an instance of the FileInput class, which can be iterated.    
    The parameters are passed to the constructor of the FileInput class.
    The returned instance, in addition to being an iterator,
    keeps global state for the functions of this module,.
files:                  #文件的路径列表,默认是stdin方式,多文件['1.txt','2.txt',...]
inplace:                #是否将标准输出的结果写回文件,默认不取代,即不写入文件
backup:                 #备份文件的扩展名,只指定扩展名,如.bak。如果该文件的备份文件已存在,则会自动覆盖。
bufsize:                #缓冲区大小,默认为0,如果文件很大,可以修改此参数,一般默认即可
mode:                   #读写模式,默认为只读
openhook:               #该钩子用于控制打开的所有文件,比如说编码方式等;
isfirstline()
    Returns true the line just read is the first line of its file,
    otherwise returns false.
注:主要是当一次性读取多个文件时,区分不同的文件

isstdin()
    Returns true if the last line was read from sys.stdin,
    otherwise returns false.
判断最后一行是否从stdin中读取,stdin指标准输入, 默认是从键盘输入
lineno()
    Return the cumulative line number of the line that has just been read.
    Before the first line has been read, returns 0. After the last line
    of the last file has been read, returns the line number of that line.
返回当前已经读取的行的数量(或者序号), 是累计的行数
nextfile()
    Close the current file so that the next iteration will read the first
    line from the next file (if any); lines not read from the file will
    not count towards the cumulative line count. The filename is not
    changed until after the first line of the next file has been read.
    Before the first line has been read, this function has no effect;
    it cannot be used to skip the first file. After the last line of the
    last file has been read, this function has no effect.

3. 常见例子
3.1 利用fileinput读取一个文件所有行

import fileinput
with fileinput.input('D:\\HeadFirstPython\\chapter1\\data.txt') as f:for line in f:print(fileinput.filename(),'|','Line Number:',fileinput.filelineno(),'|: ',line, end = "")
output:

D:\HeadFirstPython\chapter1\data.txt | Line Number: 1 |:  Perl
D:\HeadFirstPython\chapter1\data.txt | Line Number: 2 |:  Java
D:\HeadFirstPython\chapter1\data.txt | Line Number: 3 |:  C/C++
D:\HeadFirstPython\chapter1\data.txt | Line Number: 4 |:  Shell
命令行方式:
#test.py
import fileinput
for line in fileinput.input():print(fileinput.filename(),'|','Line Number:',fileinput.lineno(),'|: ',line, end = "")
output:

D:\HeadFirstPython\chapter1>python.exe 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
3.2 利用fileinput对多文件操作,并原地修改内容
3.2.1 文件内容:

#test02.py
import fileinput
def process(line):return(line.rstrip() + " line")file_names = ['D:\\HeadFirstPython\\chapter1\\1.txt', 'D:\\HeadFirstPython\\chapter1\\2.txt']
for line in fileinput.input(file_names, inplace = 1):print(process(line))
直接F5, 或者进入命令行
D:\HeadFirstPython\chapter1>python.exe test02.py
得到结果是1.txt/2.txt中变成如下:
1.txt
first line
second line2.txt
third line 
fourth line 
注:如果inpalce = False,则不会向1.txt/2.txt中插入,而会直接打印到屏幕
3.2.2 命令行方式:
#test03.py
import fileinput
def process(line):return(line.rstrip() + ' line') 
for line in fileinput.input(inplace = True):print(process(line))
#执行命令:
D:\HeadFirstPython\chapter1>python.exe test03.py 1.txt 2.txt
3.3 利用fileinput实现文件内容替换,并将原文件作备份
#test04.py
import fileinput
for line in fileinput.input("D:\\HeadFirstPython\\chapter1\\data.txt", backup = ".bak", inplace = 1):print(line.rstrip().replace("Python", "Perl"))

output:

Perl
Java
C/C++
Shell
3.4 利用fileinput对文件简单处理
#FileName: test.py
import sys
import fileinputfor line in fileinput.input('150827.txt'):sys.stdout.write('=> ')sys.stdout.write(line)
#注:print是对sys.stout.write的友好封装, 并且print可以指定sep,但是sys.stout.write也有它自己的优势
3.5 利用fileinput批处理文件
import glob
import fileinput
a = glob.iglob('15*.txt')
for line in fileinput.input(a):if fileinput.isfirstline():print('-'*20, 'Reading %s...' % fileinput.filename(), '-'*20)print(str(fileinput.lineno()) + ': ' + line.upper())
#备注:glob中就两个知识点:glob和iglob,其中iglob把结果变成生成器iterator
3.6 利用fileinput及re做日志分析: 提取所有含日期的行
import re
import fileinput
import syspattern = '\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'
#pattern = '1970-01-01 13:45:30'
for line in fileinput.input('error.log', backup = '.bak', inplace = False):if re.findall(pattern,line):sys.stdout.write('=>')sys.stdout.write(line)
测试结果:

=>1970-01-01 13:45:30  Error: **** Due to System1 Disk spacke not enough...
=>1970-01-02 13:45:30  Error: **** Due to System2 Disk spacke not enough...
=>1970-01-03 10:20:30  Error: **** Due to System3 Out of Memory...
=>1970-01-04 10:20:30  Error: **** Due to System4 Out of Memory...
3.7 利用fileinput及re做分析: 提取符合条件的电话号码
#---样本文件: phone.txt---
010-110-12345
800-333-1234
010-99999999
05718888888
021-88888888
#---测试脚本: test.py---
import re
import fileinputpattern = '[010|021]-\d{8}'  #提取区号为010或021电话号码,格式:010-12345678
#pattern = '010-110-12345'
for line in fileinput.input('phone.txt'):if re.findall(pattern,line):print('=' * 50)print('Filename:'+ fileinput.filename()+ ' | Line Number:'+str(fileinput.lineno())+ ' | '+ line, end = '')
output:

==================================================
Filename:phone.txt | Line Number:3 | 010-99999999
==================================================
Filename:phone.txt | Line Number:5 | 021-88888888
#注:print中, '+' 和 ',' 都可以, 但是','打印出来一个空格, 而'+'则没有
3.8 csv和fileinput比较:
import csv,fileinputwith open("app_dim_pop_phy_vender.csv") as csvfile:reader = csv.reader(csvfile)#print(reader)for row in reader:vender_id,vender_name,cx_dept_id,company_id,company_name = row#print(vender_id + '\t' + vender_name + '\t' + cx_dept_id + '\t' + company_id + '\t' + company_name)for row in fileinput.input("app_dim_pop_phy_vender.csv"):vender_id,vender_name,cx_dept_id,company_id,company_name = row.split(',')print(vender_id + '\t' + vender_name + '\t' + cx_dept_id + '\t' + company_id + '\t' + company_name)














这篇关于Python之fileinput的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/889983

相关文章

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

Python中bisect_left 函数实现高效插入与有序列表管理

《Python中bisect_left函数实现高效插入与有序列表管理》Python的bisect_left函数通过二分查找高效定位有序列表插入位置,与bisect_right的区别在于处理重复元素时... 目录一、bisect_left 基本介绍1.1 函数定义1.2 核心功能二、bisect_left 与

Python使用Tkinter打造一个完整的桌面应用

《Python使用Tkinter打造一个完整的桌面应用》在Python生态中,Tkinter就像一把瑞士军刀,它没有花哨的特效,却能快速搭建出实用的图形界面,作为Python自带的标准库,无需安装即可... 目录一、界面搭建:像搭积木一样组合控件二、菜单系统:给应用装上“控制中枢”三、事件驱动:让界面“活”

VSCode设置python SDK路径的实现步骤

《VSCode设置pythonSDK路径的实现步骤》本文主要介绍了VSCode设置pythonSDK路径的实现步骤,包括命令面板切换、settings.json配置、环境变量及虚拟环境处理,具有一定... 目录一、通过命令面板快速切换(推荐方法)二、通过 settings.json 配置(项目级/全局)三、

Python struct.unpack() 用法及常见错误详解

《Pythonstruct.unpack()用法及常见错误详解》struct.unpack()是Python中用于将二进制数据(字节序列)解析为Python数据类型的函数,通常与struct.pa... 目录一、函数语法二、格式字符串详解三、使用示例示例 1:解析整数和浮点数示例 2:解析字符串示例 3:解

Python程序打包exe,单文件和多文件方式

《Python程序打包exe,单文件和多文件方式》:本文主要介绍Python程序打包exe,单文件和多文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python 脚本打成exe文件安装Pyinstaller准备一个ico图标打包方式一(适用于文件较少的程

Macos创建python虚拟环境的详细步骤教学

《Macos创建python虚拟环境的详细步骤教学》在macOS上创建Python虚拟环境主要通过Python内置的venv模块实现,也可使用第三方工具如virtualenv,下面小编来和大家简单聊聊... 目录一、使用 python 内置 venv 模块(推荐)二、使用 virtualenv(兼容旧版 P

python如何生成指定文件大小

《python如何生成指定文件大小》:本文主要介绍python如何生成指定文件大小的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python生成指定文件大小方法一(速度最快)方法二(中等速度)方法三(生成可读文本文件–较慢)方法四(使用内存映射高效生成

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3

Python验证码识别方式(使用pytesseract库)

《Python验证码识别方式(使用pytesseract库)》:本文主要介绍Python验证码识别方式(使用pytesseract库),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录1、安装Tesseract-OCR2、在python中使用3、本地图片识别4、结合playwrigh