How to read specific lines of a large csv file

后端 未结 4 1108
被撕碎了的回忆
被撕碎了的回忆 2021-01-02 10:21

I am trying to read some specific rows of a large csv file, and I don\'t want to load the whole file into memory. The index of the specific rows are given in a list L

4条回答
  •  星月不相逢
    2021-01-02 11:11

    Just to sum up the great ideas, I ended up using something like this: L can be sorted relatively quickly, and in my case it was actually already sorted. So, instead of several membership checks in L it pays off to sort it and then only check each index against the first entry of it. Here is my piece of code:

    count=0
    with open('~/file.csv') as f:
        r = csv.DictReader(f)
        for row in r:
            count += 1
            if L == []:
                break
            elif count == L[0]:
                print (row)
                L.pop(0)
    

    Note that this stops as soon as we've gone through L once.

提交回复
热议问题