仅使用python读取csv文件中的某些行 [英] Only read certain rows in a csv file with python
问题描述
我只想读取整个csv文件中从csv文件中的某一行开始的一定数量的行,而无需进行迭代以达到该特定点.
I want to read only a certain amount of rows starting from a certain row in a csv file without iterating over the whole csv file to reach this certain point.
可以说我有一个包含100行的csv文件,我只想读取50至60行.我不想从第1行到49进行迭代以到达第50行以开始读取.我可以通过seek()以某种方式实现这一目标吗?
Lets say i have a csv file with 100 rows and i want to read only row 50 to 60. I dont want to iterate from row 1 to 49 to reach row 50 to start reading. Can i somehow achieve this with seek()?
例如:寻求第50行从50读到60
For example: Seek to row 50 read from 50 to 60
下一次:寻求第27行阅读27至34等等
next time: seek to row 27 read 27 to 34 and so on
因此,不仅在文件中不断搜索,而且在文件之后也搜索.
So not only seeking continuesly forward through the file but also backwards.
非常感谢
推荐答案
您可以使用 chunksize
import pandas as pd
chunksize = 10 ** 6
for chunk in pd.read_csv(filename, chunksize=chunksize):
process(chunk)
这篇关于仅使用python读取csv文件中的某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!