Python如何優雅地倒著讀檔案?

時間 2021-06-02 23:32:21

1樓:西瓜甜

from

collections

import

deque

with

open

(file_name

,'r'

,encoding

='utf-8')as

fb:dq=

deque(fb

)# 讀取方式一: 從尾讀到頭,讀完

whiledq:

last_row=dq

.pop

()# 讀取方式二:從尾讀開始讀,到指定條件後結束whiledq:

last_row=dq

.pop

()if

...:

break

deque 是乙個雙端佇列,可以接收乙個可迭代物件,之後轉換為 deque 的乙個例項。例項的 .pop() 方法會取出佇列中的最後乙個值。

當佇列中的資料被讀完, 佇列就被 python 判定為 False

>>> from collections import deque>>> dq = deque('123')>>> type(dq)

>>> bool(dq)

True

>>> dq.pop()

'3'>>> dq.pop()

'2'>>> dq.pop()

'1'>>> bool(dq)

False

2樓:liusl

如果想實現的需求是:

test.log:

first line

last line

Output:

last line

first line

自己寫了乙個:

with open('test.log', 'r') as fr:

# 跳到檔案末尾

fr.seek(0, 2)

line = ''

while True如果到了檔案頭部,列印第一行,跳出迴圈if fr.tell() == 1print line[::-1break

else每次從當前相對位置向前位移2,讀1個字元fr.seek(-2, 1char = fr.read(1如果字元為換行符,列印整行,如果不是,把字元加到行末尾if char !

= '\n'line += charelseprint line[::-1line = ''

3樓:廖雷

defread_reverse

(filename):f

=open

(filename)f

.seek(0

,2)last_position=f

.tell

()while

True

:line=f

.readline

()current_position=f

.tell()i

=1while

current_position

==last_position:if

len(

line)==

current_position

:yield

line

returni+=

0.5f

.seek

(max

(int(-

72*i),

-current_position),1

)line=f

.readline

()current_position=f

.tell

()while

current_position

!=last_position

:line=f

.readline

()current_position=f

.tell

()yield

line

last_position

=last_position

-len

(line)f

.seek

(max(-

72,-last_position)-

len(

line),1

)自己寫的生成器版倒著讀,效率極高,貼出來分享給大家。

4樓:黃哥

方法之一:

from collections import dequedef tail(filename, n=1039;Return the last n lines of a file'while Truelines = '

'.join(list(deque(open(filename), nself.write_message(linesif linestime.

sleep(0.5continue

tail('ip.txt')

方法二:用f.seek(offset, from_what)自己搜尋解決方法。

方法三:

如果日誌檔案在10G以下,用生成器順序讀,加if 判斷效率也很高。

5樓:石頭三顆

不要隨意使用優雅這個詞

讀完後倒敘一下就好了,優雅與否不曉得。

a='testing中文資料'

a[::-1]

返回資訊:

'據數文中gnitset'

如果是按行書寫的日誌檔案,一次性讀入的時候就是乙個list,比如:

a=['前天','昨天','今天']

a[::-1]

Out[5]: ['今天', '昨天', '前天']

來個實際的日誌檔案試試:(隨便在C盤抓了個日誌檔案)

f=open('c:/TimeEventNSIS.log','r')

a=f.readlines()

a[::-1]

['2014/11/02 22:15:04-1414937704There does not exist 360\t\tRestore360KiFast\n',

'2014/11/02 22:15:04-1414937704strDllPath:

\t\tC:\\Program Files\\BaiduAn3.0\\BaiduAn\\3.

0.0.3971\\bdmantivirus\\BDKitUtils.

dll\n',

'2014/11/02 22:15:04-1414937704Start\t\tRestore360KiFast\n',

'2014/11/02 17:06:07-1414919167There does not exist 360\t\tRestore360KiFast\n',

'2014/11/02 17:06:07-1414919167strDllPath:

\t\tC:\\Program Files\\BaiduAn3.0\\BaiduAn\\3.

0.0.3971\\bdmantivirus\\BDKitUtils.

dll\n',

'2014/11/02 17:06:07-1414919167Start\t\tRestore360KiFast\n',

'2014/11/01 21:47:07-1414849627There does not exist 360\t\tRestore360KiFast\n',

'2014/11/01 21:47:07-1414849627strDllPath:

\t\tC:\\Program Files\\BaiduAn3.0\\BaiduAn\\3.

0.0.3971\\bdmantivirus\\BDKitUtils.

dll\n',

'2014/11/01 21:47:07-1414849627Start\t\tRestore360KiFast\n',

'2014/11/01 21:10:45-1414847445There does not exist 360\t\tRestore360KiFast\n',

'2014/11/01 21:10:45-1414847445strDllPath:

\t\tC:\\Program Files\\BaiduAn3.0\\BaiduAn\\3.

0.0.3971\\bdmantivirus\\BDKitUtils.

dll\n',

'2014/11/01 21:10:45-1414847445Start\t\tRestore360KiFast\n']

如何優雅地倒啤酒?

必須得是後半夜,必須得是露天的燒烤攤,必須得是光膀子金鍊子滿臉橫肉一身紋身,必須得有濃妝豔抹燙頭短裙小妹一名。大哥緩緩訴說作為社會人這幾多年來的浮沉與榮耀,當年從南天門砍到南京路的輝煌。小妹剝完蒜,忙不溜的給大哥倒啤酒,時不時抬頭看大哥,眼裡全是崇拜。大哥也瞅著小妹,眼裡滿是化不開的濃情蜜意。老子今...

有沒有 優雅的 python 倒著一行一行讀大檔案的例子

Justin Z file read backwards file read backwards 2.0.0 documentation 乙個現成的包,安裝 pip install file read backwards 官方Python 3的例子 from file read backwards ...

如何優雅地離職?

王永峰 準備好辭職的理由 要表現出堅決離職的態度,但無需詳述離職的理由。外企的職業經理人已經習慣了員工的來來往往,他們需要乙個能夠說服自己和公司的理由,而不一定需要了解你真正離開的原因,以下幾種原因供參考 1 職業規劃 在企業中無法實現我個人的職業生涯計畫,想尋求新的發展平台。2 職位的生命週期已到...