简体   繁体   English

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

[英]read specific line in csv file , python

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

You can use linecache.getline :您可以使用linecache.getline

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

Get line lineno from file named filename.从名为 filename 的文件中获取 linelineno。 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)

Or use the consume recipe from itertools to move the pointer:或者使用 itertools 中的消耗配方来移动指针:

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)

Alternatively you can leverage the nrows and skiprows argument in pandas或者,您可以利用熊猫中的nrowsskiprows参数

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

remember skiprows can be a list so if you need the header use记住skiprows可以是一个列表,所以如果你需要标题使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM