[英]read specific line in csv file , python
在使用 python 的CSV
文件中,我们可以逐行或逐行读取所有文件,我想读取特定行(行号 24 示例)而不读取所有文件和所有行。
您可以使用linecache.getline :
linecache.getline(文件名,lineno[,module_globals])
从名为 filename 的文件中获取 linelineno。 这个函数永远不会引发异常——它会在出现错误时返回 ''(找到的行将包含终止换行符)。
import linecache
line = linecache.getline("foo.csv",24)
或者使用 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)
或者,您可以利用熊猫中的nrows
和skiprows
参数
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)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.