Python中fileinput模組介紹,pythonfileinput

來源:互聯網
上載者:User

Python中fileinput模組介紹,pythonfileinput

fileinput模組可以對一個或多個檔案中的內容進行迭代、遍曆等操作。

該模組的input()函數有點類似檔案readlines()方法,區別在於:

前者是一個迭代對象,即每次只產生一行,需要用for迴圈迭代。

後者是一次性讀取所有行。在碰到大檔案的讀取時,前者無疑效率更高效。

用fileinput對檔案進行迴圈遍曆,格式化輸出,尋找、替換等操作,非常方便。

【典型用法】

import fileinputfor line in fileinput.input():    process(line)

【基本格式】

fileinput.input([files[, inplace[, backup[, bufsize[, mode[, openhook]]]]]])

【預設格式】

fileinput.input (files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)

files:                  #檔案的路徑列表,預設是stdin方式,多檔案['1.txt','2.txt',...]inplace:                #是否將標準輸出的結果寫迴文件,預設不取代backup:                 #備份檔案的副檔名,只指定副檔名,如.bak。如果該檔案的備份檔案已存在,則會自動覆蓋。bufsize:                #緩衝區大小,預設為0,如果檔案很大,可以修改此參數,一般預設即可mode:                   #讀寫入模式,預設為唯讀openhook:               #該鉤子用於控制開啟的所有檔案,比如說編碼方式等;
【常用函數】
fileinput.input()       #返回能夠用於for迴圈遍曆的對象fileinput.filename()    #返回當前檔案的名稱fileinput.lineno()      #返回當前已經讀取的行的數量(或者序號)fileinput.filelineno()  #返回當前讀取的行的行號fileinput.isfirstline() #檢查當前行是否是檔案的第一行fileinput.isstdin()     #判斷最後一行是否從stdin中讀取fileinput.close()       #關閉隊列

【常見例子】

  • 例子01: 利用fileinput讀取一個檔案所有行
>>> import fileinput>>> for line in fileinput.input('data.txt'):        print line,#輸出結果PythonJava C/C++Shell

命令列方式:

#test.pyimport fileinputfor line in fileinput.input():    print fileinput.filename(),'|','Line Number:',fileinput.lineno(),'|: ',linec:>python test.py data.txtdata.txt | Line Number: 1 |:  Pythondata.txt | Line Number: 2 |:  Javadata.txt | Line Number: 3 |:  C/C++data.txt | Line Number: 4 |:  Shell
  • 例子02: 利用fileinput對多檔案操作,並原地修改內容
#test.py#---樣本檔案---c:\Python27>type 1.txtfirstsecondc:\Python27>type 2.txtthirdfourth#---樣本檔案---import fileinputdef process(line):    return line.rstrip() + ' line'for line in fileinput.input(['1.txt','2.txt'],inplace=1):    print process(line)#---結果輸出---c:\Python27>type 1.txtfirst linesecond linec:\Python27>type 2.txtthird linefourth line#---結果輸出---
命令列方式:
#test.pyimport fileinputdef 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
  • 例子03: 利用fileinput實現檔案內容替換,並將原檔案作備份
#樣本檔案:#data.txtPythonJavaC/C++Shell#FileName: test.pyimport fileinputfor line in fileinput.input('data.txt',backup='.bak',inplace=1):    print line.rstrip().replace('Python','Perl')  #或者print line.replace('Python','Perl'),    #最後結果:#data.txtPythonJavaC/C++Shell#並產生:#data.txt.bak檔案
  • 例子04: 利用fileinput將CRLF檔案轉為LF
import fileinputimport sysfor line in fileinput.input(inplace=True):    #將Windows/DOS格式下的文字檔轉為Linux的檔案    if line[-2:] == "\r\n":          line = line + "\n"    sys.stdout.write(line)
  • 例子05: 利用fileinput對檔案簡單處理
#FileName: test.pyimport sysimport fileinputfor 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 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!
  • 例子06: 利用fileinput批次檔
#---測試檔案: test.txt test1.txt test2.txt test3.txt---#---指令檔: test.py---import fileinputimport globfor line in fileinput.input(glob.glob("test*.txt")):    if fileinput.isfirstline():        print '-'*20, 'Reading %s...' % fileinput.filename(), '-'*20    print str(fileinput.lineno()) + ': ' + line.upper(),        #---輸出結果:>>> -------------------- Reading test.txt... --------------------1: AAAAA2: BBBBB3: CCCCC4: DDDDD5: FFFFF-------------------- Reading test1.txt... --------------------6: FIRST LINE7: SECOND LINE-------------------- Reading test2.txt... --------------------8: THIRD LINE9: FOURTH LINE-------------------- Reading test3.txt... --------------------10: THIS IS LINE 111: THIS IS LINE 212: THIS IS LINE 313: THIS IS LINE 4
  • 例子07: 利用fileinput及re做日誌分析: 提取所有含日期的行
#--樣本檔案--aaa1970-01-01 13:45:30  Error: **** Due to System Disk spacke not enough...bbb1970-01-02 10:20:30  Error: **** Due to System Out of Memory...ccc#---測試指令碼---import reimport fileinputimport syspattern = '\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)#---測試結果---=> 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...
  • 例子08: 利用fileinput及re做分析: 提取合格電話號碼
#---樣本檔案: phone.txt---010-110-12345800-333-1234010-9999999905718888888021-88888888#---測試指令碼: test.py---import reimport fileinputpattern = '[010|021]-\d{8}'  #提取區號為010或021電話號碼,格式:010-12345678for line in fileinput.input('phone.txt'):    if re.search(pattern,line):        print '=' * 50        print 'Filename:'+ fileinput.filename()+' | Line Number:'+str(fileinput.lineno())+' | '+line,#---輸出結果:--->>> ==================================================Filename:phone.txt | Line Number:3 | 010-99999999==================================================Filename:phone.txt | Line Number:5 | 021-88888888>>> 
  • 例子09: 利用fileinput實作類別似於grep的功能
import sysimport reimport fileinputpattern= 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檔案中,含import re字樣的addressBook.py  2   import readdressBook1.py 10  import readdressBook2.py 18  import retest.py         238 import re
  • 例子10: 利用fileinput做正則替換
#---測試樣本: input.txt* [Learning Python](#author:Mark Lutz)    #---測試指令碼: test.pyimport fileinputimport re for line in fileinput.input():    line = re.sub(r'\* \[(.*)\]\(#(.*)\)', r'<h2 id="\2">\1</h2>', line.rstrip())    print(line)#---輸出結果:c:\Python27>python test.py input.txt<h2 id="author:Mark Lutz">Learning Python</h2>

  • 例子11: 利用fileinput做正則替換,不同字模組之間的替換
#---測試樣本:test.txt[@!$First]&[*%-Second]&[Third]#---測試指令碼:test.pyimport reimport fileinputregex = re.compile(r'^([^&]*)(&)([^&]*)(&)([^&]*)')#整行以&分割,要實現[@!$First]與[*%-Second]互換for line in fileinput.input('test.txt',inplace=1,backup='.bak'):    print regex.sub(r'\3\2\1\4\5',line),#---輸出結果:[*%-Second]&[@!$First]&[Third]
  • 例子12: 利用fileinput根據argv命令列輸入做替換
#---樣本資料: host.txt# localhost is used to configure the loopback interface# when the system is booting.  Do not change this entry.127.0.0.1      localhost192.168.100.2  www.test2.com192.168.100.3  www.test3.com192.168.100.4  www.test4.com#---測試指令碼: test.pyimport sysimport fileinputsource = 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    #---輸出結果:    c:\>python test.py 192.168.100 127.0.0 host.txt#將host檔案中,所有192.168.100轉換為:127.0.0127.0.0.1  localhost127.0.0.2  www.test2.com127.0.0.3  www.test3.com127.0.0.4  www.test4.com







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.