如何存储CSV文件中的行?
在我的CSV文件中,我有5行描述该文件。我需要存储第三行,但忽略其余行。我已经成功地编写了一个忽略所有描述标题的代码,但是我怎么能只存储一行呢? 我编写这段代码是为了忽略所有描述标题如何存储CSV文件中的行?,csv,python-3.x,Csv,Python 3.x,在我的CSV文件中,我有5行描述该文件。我需要存储第三行,但忽略其余行。我已经成功地编写了一个忽略所有描述标题的代码,但是我怎么能只存储一行呢? 我编写这段代码是为了忽略所有描述标题 for row in range(6): csvfile.readline() 您可以使用获取第8行,这将是五个标题行之后的第3行,即第8行或索引7: from itertools import islice line = next(islice(csvfile, 7, 8)) # start=i
for row in range(6):
csvfile.readline()
您可以使用获取第8行,这将是五个标题行之后的第3行,即第8行或索引7:
from itertools import islice
line = next(islice(csvfile, 7, 8)) # start=index 7, end= index 8 so one line
您也可以使用从文件中获取单行,但这不是最有效的方法
如果您有一个非常大的文件,跳过n行也可能很有用:
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)
您可以使用获取第8行,这将是五个标题行之后的第3行,即第8行或索引7:
from itertools import islice
line = next(islice(csvfile, 7, 8)) # start=index 7, end= index 8 so one line
您也可以使用从文件中获取单行,但这不是最有效的方法
如果您有一个非常大的文件,跳过n行也可能很有用:
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)