本文目錄一覽:
- 1、python 怎麼讀取文件每行的開頭和末尾?
- 2、如何用python最快的獲取大文件的最後幾行
- 3、python 怎樣或讀取一個文件的最後一行
- 4、如何用python獲取文件的最後一行,文件可能會比較大
- 5、如何利用python讀取特定目錄下的特定文件的倒數兩行
python 怎麼讀取文件每行的開頭和末尾?
text=”””
16 wyp1 23 131212121212
17 wyp2 24 134535353535
18 wyp3 25 132453535353
19 wyp4 26 154243434355
20 wyp 25 13188888888888
21 test 30 13888888888888
22 zs 34 899314121
“””
text_arr = text.split(“\n”)#根據換行符拆分字符串
# print(text_arr)
#content_dict = {}#字典,用來裝結果
for i in text_arr:
if i == “”:#如果這個內容是空的,則略過,繼續下一個
continue
i_arr = i.split(” “)#根據空格拆分字符串
content_dict[i_arr[0]] = i_arr[-1]#將字符串列表的第一個位置作為鍵,最後一個位置的內容作為值
print(content_dict)
兩次print的結果
ps:圖片好像不是高清的…….
如何用python最快的獲取大文件的最後幾行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
import os
import sys
def get_last_n_lines(logfile, n):
blk_size_max = 4096
n_lines = []
with open(logfile, ‘rb’) as fp:
fp.seek(0, os.SEEK_END)
cur_pos = fp.tell()
while cur_pos 0 and len(n_lines) n:
blk_size = min(blk_size_max, cur_pos)
fp.seek(cur_pos – blk_size, os.SEEK_SET)
blk_data = fp.read(blk_size)
assert len(blk_data) == blk_size
lines = blk_data.split(‘\n’)
# adjust cur_pos
if len(lines) 1 and len(lines[0]) 0:
n_lines[0:0] = lines[1:]
cur_pos -= (blk_size – len(lines[0]))
else:
n_lines[0:0] = lines
cur_pos -= blk_size
fp.seek(cur_pos, os.SEEK_SET)
if len(n_lines) 0 and len(n_lines[-1]) == 0:
del n_lines[-1]
return n_lines[-n:]
def main():
if len(sys.argv) != 3:
sys.exit(‘usage: %s logfile n_lines’ % sys.argv[0])
for line in get_last_n_lines(sys.argv[1], int(sys.argv[2])):
print line
if __name__ == ‘__main__’:
main()
Linux平台有一個tail命令,tail -f filename.log 就會打印文件最後新增加的內容
python 怎樣或讀取一個文件的最後一行
有兩種情況,
1,文件比較大時,一行一行循環直到最後一行,讀取最後一行;
targetLine = “”;
lineNo = 0;
while 1:
mLine = file.readline();
if not mLine:
break;
lineNo += 1;
if (linecount == lineNO):
targetLine = mLine;
2, 文件比較小,直接讀取全文,取最後一行數據。
targetLine = “”;
mLines = file.read();
targetLine = mLines[-1];
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.
如何用python獲取文件的最後一行,文件可能會比較大
#!/usr/bin/env python
import os
import sys
def get_last_n_lines(logfile, n):
blk_size_max = 4096
n_lines = []
with open(logfile, ‘rb’) as fp:
fp.seek(0, os.SEEK_END)
cur_pos = fp.tell()
while cur_pos 0 and len(n_lines) n:
blk_size = min(blk_size_max, cur_pos)
fp.seek(cur_pos – blk_size, os.SEEK_SET)
blk_data = fp.read(blk_size)
assert len(blk_data) == blk_size
lines = blk_data.split(‘\n’)
# adjust cur_pos
if len(lines) 1 and len(lines[0]) 0:
n_lines[0:0] = lines[1:]
cur_pos -= (blk_size – len(lines[0]))
else:
n_lines[0:0] = lines
cur_pos -= blk_size
fp.seek(cur_pos, os.SEEK_SET)
if len(n_lines) 0 and len(n_lines[-1]) == 0:
del n_lines[-1]
return n_lines[-n:]
def main():
if len(sys.argv) != 3:
sys.exit(‘usage: %s logfile n_lines’ % sys.argv[0])
for line in get_last_n_lines(sys.argv[1], int(sys.argv[2])):
print line
if __name__ == ‘__main__’:
main()
Linux平台有一個tail命令,tail -f filename.log 就會打印文件最後新增加的內容
這個也可以
如何利用python讀取特定目錄下的特定文件的倒數兩行
讀取最後2行,別信那些用readlines()的答案。那些答案,丟給你個16GB的文件就死翹翹了。老老實實用tail命令的實現方法:
用os.seek跳轉到文件末尾,os.tell判斷文件大小
設置個合適的buf size,假設是1024。循環從文件末尾os.seek往回跳buf size,判斷讀取的內容里回車符的數量,累加回車符數量
當回車符數量大於等於2的時候,停止循環。確定倒數第二個回車符的位置,os.seek到那個位置,輸出到文件末尾
這個實現還是有坑。如果文件一直在增長,那麼『最後兩行』應該是程序執行當時文件的最後兩行,步驟3里應該是『從倒數第二個回車符輸出到步驟1中獲取的文件大小位置』
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/292001.html