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

时间:2015-06-21 11:57:39

标签: python csv reader

在带有python的CSV文件中,我们可以逐行或逐行读取所有文件,我想读取特定行(第24行示例)而不读取所有文件和所有行。< / p>

2 个答案:

答案 0 :(得分:7)

您可以使用linecache.getline

linecache.getline(filename,lineno [,module_globals])

  

从名为filename的文件中获取line lineno。此函数永远不会引发异常 - 它将在错误时返回''(终止的换行符将包含在找到的行中)。

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)

答案 1 :(得分:0)

或者,您可以在熊猫中利用nrowsskiprows参数

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

记住skiprows可以是一个列表,因此如果您需要使用标头

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