Python:讀取特定行(小文件、重複文件、大型文件的不同解決方案)

問題描述

當使用for循環讀取文件時,在某些情況下,我們只想讀取特定的行,比如第26行和第30行,對於不同的情況,有3個內置特性可以實現這個目標。

When using a for loop to read a file, in some cases we only want to read specific lines, say line #26 and #30, there are 3 built-in features to achieve this goal for different cases.

For reading small files

對於小文件的快速解決辦法:

Use fileobject.readlines() or for line in fileobject as a quick solution for small files.

f = open('filename')
lines=f.readlines()
print lines[25]
print lines[29]

or:

lines = [25, 29]
i = 0
f = open('filename')
for line in f:
    if i in lines:
        print i
    i += 1

For reading many files, possible repeatedly

使用linecache是一個更優雅的解決方案,它可以快速讀取許多文件,甚至可以重複讀取。
There is a more elegant solution for extracting many lines: linecache

import linecache
linecache.getline('/etc/passwd', 4)
'sys:x:3:3:sys:/dev:/bin/sh\n'

將4改爲想要的行號,就可以了。請注意,由於計數是從零開始的,所以第4行是第5行。

Change the 4 to your desired line number, and you’re on. Note that 4 would bring the fifth line as the count is zero-based.

For large files which won’t fit into memory

當文件非常大,而且無法放入內存時,用enumerate()。注意,使用此方法可能會變慢,因爲文件是按順序讀取的。
If the file to read is big, and cause problems when read into memory or you don’t want to read the whole file in memory at once, it might be a good idea to use enumerate():

fp = open("file")
for i, line in enumerate(fp):
    if i == 25:
        # 26th line
    elif i == 29:
        # 30th line
    elif i > 29:
        break
fp.close()

Note that i == n-1 for the nth line.


In Python 2.6 or later:

with open("file") as fp:
    for i, line in enumerate(fp):
        if i == 25:
            # 26th line
        elif i == 29:
            # 30th line
        elif i > 29:
            break

整理並翻譯自:stackoverflow

https://stackoverflow.com/questions/2081836/reading-specific-lines-only?answertab=active#tab-top

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章