使用 open 函數(shù)去讀取文件,似乎是所有 Python 工程師的共識。
今天明哥要給大家推薦一個比 open 更好用、更優(yōu)雅的讀取文件方法 – 使用 fileinput
1. 從標(biāo)準(zhǔn)輸入中讀取
當(dāng)你的 Python 腳本沒有傳入任何參數(shù)時,fileinput 默認(rèn)會以 stdin 作為輸入源
1
2
3
4
5
|
# demo.py import fileinput for line in fileinput. input (): print (line) |
效果如下,不管你輸入什么,程序會自動讀取并再打印一次,像個復(fù)讀機(jī)似的。
1
2
3
4
5
6
|
$ python demo.py hello hello python python |
2. 單獨打開一個文件
腳本的內(nèi)容如下
1
2
3
4
5
|
import fileinput with fileinput. input (files = ( 'a.txt' ,)) as file : for line in file : print (f '{fileinput.filename()} 第{fileinput.lineno()}行: {line}' , end = '') |
其中 a.txt
的內(nèi)容如下
hello
world
執(zhí)行后就會輸出如下
1
2
3
|
$ python demo.py a.txt 第1行: hello a.txt 第2行: world |
需要說明的一點是,fileinput.input()
默認(rèn)使用 mode='r'
的模式讀取文件,如果你的文件是二進(jìn)制的,可以使用mode='rb'
模式。fileinput 有且僅有這兩種讀取模式。
3. 批量打開多個文件
從上面的例子也可以看到,我在 fileinput.input
函數(shù)中傳入了 files
參數(shù),它接收一個包含多個文件名的列表或元組,傳入一個就是讀取一個文件,傳入多件就是讀取多個文件。
1
2
3
4
5
|
import fileinput with fileinput. input (files = ( 'a.txt' , 'b.txt' )) as file : for line in file : print (f '{fileinput.filename()} 第{fileinput.lineno()}行: {line}' , end = '') |
a.txt
和 b.txt
的內(nèi)容分別是
1
2
3
4
5
6
|
$ cat a.txt hello world $ cat b.txt hello python |
運(yùn)行后輸出結(jié)果如下,由于 a.txt
和 b.txt
的內(nèi)容被整合成一個文件對象 file
,因此 fileinput.lineno()
只有在讀取一個文件時,才是原文件中真實的行號。
1
2
3
4
5
|
$ python demo.py a.txt 第1行: hello a.txt 第2行: world b.txt 第3行: hello b.txt 第4行: python |
如果想要在讀取多個文件的時候,也能讀取原文件的真實行號,可以使用 fileinput.filelineno()
方法
1
2
3
4
5
|
import fileinput with fileinput. input (files = ( 'a.txt' , 'b.txt' )) as file : for line in file : print (f '{fileinput.filename()} 第{fileinput.filelineno()}行: {line}' , end = '') |
運(yùn)行后,輸出如下
1
2
3
4
5
|
$ python demo.py a.txt 第1行: hello a.txt 第2行: world b.txt 第1行: hello b.txt 第2行: python |
這個用法和 glob 模塊簡直是絕配
1
2
3
4
5
6
7
|
import fileinput import glob for line in fileinput. input (glob.glob( "*.txt" )): if fileinput.isfirstline(): print ( '-' * 20 , f 'Reading {fileinput.filename()}...' , '-' * 20 ) print ( str (fileinput.lineno()) + ': ' + line.upper(), end = "") |
運(yùn)行效果如下
1
2
3
4
5
6
7
|
$ python demo.py -------------------- Reading b.txt... -------------------- 1: HELLO 2: PYTHON -------------------- Reading a.txt... -------------------- 3: HELLO 4: WORLD |
4. 讀取的同時備份文件
fileinput.input
有一個 backup 參數(shù),你可以指定備份的后綴名,比如 .bak
1
2
3
4
|
import fileinput with fileinput. input (files = ( "a.txt" ,), backup = ".bak" ) as file : for line in file : print (f '{fileinput.filename()} 第{fileinput.lineno()}行: {line}' , end = '') |
運(yùn)行的結(jié)果如下,會多出一個 a.txt.bak
文件
1
2
3
4
5
6
7
8
9
10
|
$ ls -l a.txt* -rw-r--r-- 1 MING staff 12 2 27 10:43 a.txt $ python demo.py a.txt 第1行: hello a.txt 第2行: world $ ls -l a.txt* -rw-r--r-- 1 MING staff 12 2 27 10:43 a.txt -rw-r--r-- 1 MING staff 42 2 27 10:39 a.txt.bak |
5. 標(biāo)準(zhǔn)輸出重定向替換
fileinput.input
有一個 inplace 參數(shù),表示是否將標(biāo)準(zhǔn)輸出的結(jié)果寫回文件,默認(rèn)不取代
請看如下一段測試代碼
1
2
3
4
5
6
7
|
import fileinput with fileinput. input (files = ( "a.txt" ,), inplace = True ) as file : print ( "[INFO] task is started..." ) for line in file : print (f '{fileinput.filename()} 第{fileinput.lineno()}行: {line}' , end = '') print ( "[INFO] task is closed..." ) |
運(yùn)行后,會發(fā)現(xiàn)在 for 循環(huán)體內(nèi)的 print 內(nèi)容會寫回到原文件中了。而在 for 循環(huán)體外的 print 則沒有變化。
1
2
3
4
5
6
7
8
9
10
11
|
$ cat a.txt hello world $ python demo.py [INFO] task is started... [INFO] task is closed... $ cat a.txt a.txt 第1行: hello a.txt 第2行: world |
利用這個機(jī)制,可以很容易的實現(xiàn)文本替換。
1
2
3
4
5
6
7
8
|
import sys import fileinput for line in fileinput. input (files = ( 'a.txt' , ), inplace = True ): #將Windows/DOS格式下的文本文件轉(zhuǎn)為Linux的文件 if line[ - 2 :] = = "\r\n" : line = line + "\n" sys.stdout.write(line) |
附:如何實現(xiàn) DOS 和 UNIX 格式互換以供程序測試,使用 vim 輸入如下指令即可
DOS轉(zhuǎn)UNIX::setfileformat=unix
UNIX轉(zhuǎn)DOS::setfileformat=dos
6. 不得不介紹的方法
如果只是想要 fileinput
當(dāng)做是替代 open 讀取文件的工具,那么以上的內(nèi)容足以滿足你的要求。
fileinput.filenam()
返回當(dāng)前被讀取的文件名。 在第一行被讀取之前,返回 None
。
fileinput.fileno()
返回以整數(shù)表示的當(dāng)前文件“文件描述符”。 當(dāng)未打開文件時(處在第一行和文件之間),返回 -1
。
fileinput.lineno()
返回已被讀取的累計行號。 在第一行被讀取之前,返回 0
。 在最后一個文件的最后一行被讀取之后,返回該行的行號。
fileinput.filelineno()
返回當(dāng)前文件中的行號。 在第一行被讀取之前,返回 0
。 在最后一個文件的最后一行被讀取之后,返回此文件中該行的行號。
但若要想基于 fileinput 來做一些更加復(fù)雜的邏輯,也許你會需要用到如下這幾個方法
fileinput.isfirstline()
如果剛讀取的行是其所在文件的第一行則返回 True
,否則返回 False
。
fileinput.isstdin()
如果最后讀取的行來自 sys.stdin
則返回 True
,否則返回 False
。
fileinput.nextfile()
關(guān)閉當(dāng)前文件以使下次迭代將從下一個文件(如果存在)讀取第一行;不是從該文件讀取的行將不會被計入累計行數(shù)。 直到下一個文件的第一行被讀取之后文件名才會改變。 在第一行被讀取之前,此函數(shù)將不會生效;它不能被用來跳過第一個文件。 在最后一個文件的最后一行被讀取之后,此函數(shù)將不再生效。
fileinput.close()
關(guān)閉序列。
7. 進(jìn)階一點的玩法
在 fileinput.input()
中有一個 openhook
的參數(shù),它支持用戶傳入自定義的對象讀取方法。
若你沒有傳入任何的勾子,fileinput 默認(rèn)使用的是 open 函數(shù)。
fileinput
為我們內(nèi)置了兩種勾子供你使用
1. fileinput.hook_compressed(*filename*, *mode*)
使用 gzip
和 bz2
模塊透明地打開 gzip 和 bzip2 壓縮的文件(通過擴(kuò)展名 '.gz'
和 '.bz2'
來識別)。 如果文件擴(kuò)展名不是 '.gz'
或 '.bz2'
,文件會以正常方式打開(即使用 open()
并且不帶任何解壓操作)。使用示例:
fi = fileinput.FileInput(openhook=fileinput.hook_compressed)
2. fileinput.hook_encoded(*encoding*, *errors=None*)
返回一個通過 open()
打開每個文件的鉤子,使用給定的 encoding 和 errors 來讀取文件。使用示例:
fi = fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8", "surrogateescape"))
如果你自己的場景比較特殊,以上的三種勾子都不能滿足你的要求,你也可以自定義。
這邊我舉個例子來拋磚引玉下
假如我想要使用 fileinput 來讀取網(wǎng)絡(luò)上的文件,可以這樣定義勾子。
先使用 requests 下載文件到本地
再使用 open 去讀取它
1
2
3
4
5
6
7
8
|
def online_open(url, mode): import requests r = requests.get(url) filename = url.split( "/" )[ - 1 ] with open (filename, 'w' ) as f1: f1.write(r.content.decode( "utf-8" )) f2 = open (filename, 'r' ) return f2 |
直接將這個函數(shù)傳給 openhoos 即可
1
2
3
4
5
6
|
import fileinput file_url = 'https://www.csdn.net/robots.txt' with fileinput. input (files = (file_url,), openhook = online_open) as file : for line in file : print (line, end = "") |
運(yùn)行后按預(yù)期一樣將 CSDN 的 robots 的文件打印了出來
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
User-agent: * Disallow: /scripts Disallow: /public Disallow: /css/ Disallow: /images/ Disallow: /content/ Disallow: /ui/ Disallow: /js/ Disallow: /scripts/ Disallow: /article_preview .html* Disallow: /tag/ Disallow: /*?* Disallow: /link/ Sitemap: https: //www .csdn.net /sitemap-aggpage-index .xml Sitemap: https: //www .csdn.net /article/sitemap .txt |
8. 列舉一些實用案例
案例一:讀取一個文件所有行
1
2
3
|
import fileinput for line in fileinput. input ( 'data.txt' ): print (line, end = "") |
案例二:讀取多個文件所有行
1
2
3
4
5
6
7
|
import fileinput import glob for line in fileinput. input (glob.glob( "*.txt" )): if fileinput.isfirstline(): print ( '-' * 20 , f 'Reading {fileinput.filename()}...' , '-' * 20 ) print ( str (fileinput.lineno()) + ': ' + line.upper(), end = "") |
案例三:利用fileinput將CRLF文件轉(zhuǎn)為LF
1
2
3
4
5
6
7
8
|
import sys import fileinput for line in fileinput. input (files = ( 'a.txt' , ), inplace = True ): #將Windows/DOS格式下的文本文件轉(zhuǎn)為Linux的文件 if line[ - 2 :] = = "\r\n" : line = line + "\n" sys.stdout.write(line) |
案例四:配合 re 做日志分析:取所有含日期的行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#--樣本文件--:error.log aaa 1970 - 01 - 01 13 : 45 : 30 Error: * * * * Due to System Disk spacke not enough... bbb 1970 - 01 - 02 10 : 20 : 30 Error: * * * * Due to System Out of Memory... ccc #---測試腳本--- 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) #---測試結(jié)果--- = > 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... |
案例五:利用fileinput實現(xiàn)類似于grep的功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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 #查找所有py文件中,含import re字樣的 addressBook.py 2 import re addressBook1.py 10 import re addressBook2.py 18 import re test.py 238 import re |
9. 寫在最后
fileinput 是對 open 函數(shù)的再次封裝,在僅需讀取數(shù)據(jù)的場景中, fileinput 顯然比 open 做得更專業(yè)、更人性,當(dāng)然在其他有寫操作的復(fù)雜場景中,fileinput 就無能為力啦,本身從 fileinput 的命名上就知道這個模塊只專注于輸入(讀)而不是輸出(寫)。
以上就是Python讀取文件比open快十倍的庫fileinput的詳細(xì)內(nèi)容,更多關(guān)于Python讀取文件庫fileinput的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://blog.csdn.net/weixin_36338224/article/details/114282594