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

你可能感兴趣的:(Coding,python)