读取 csv 文件中的特定行,python

read specific line in csv file , python

CSV 文件中 python 我们可以逐行或逐行读取所有文件,我想读取特定行(行号 24 示例)而不读取所有文件和所有的行。

您可以使用 linecache.getline:

linecache.getline(文件名, lineno[ module_globals])

Get line lineno from file named filename. This function will never raise an exception — it will return '' on errors (the terminating newline character will be included for lines that are found).

import linecache


line = linecache.getline("foo.csv",24)

或者使用 itertools 中的 consume recipe 来移动指针:

import collections
from itertools import islice

def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

with open("foo.csv") as f:
    consume(f,23)
    line = next(f)

或者,您可以利用 pandas

中的 nrowsskiprows 参数
line_number = 30
pd.read_csv('big.csv.gz', sep = "\t", nrows = 1, skiprows = line_number - 1)

记住 skiprows 可以是一个列表,所以如果你需要 header 使用

pd.read_csv('big.csv.gz', sep = "\t", nrows = 1, skiprows = list(range(1, line_number - 1)))