读取csv并跳转到特定索引

时间:2018-10-04 18:10:36

标签: python-3.x csv

读取csv文件时遇到问题。我想读取一行csv文件,然后跳到特定行(例如3)。到目前为止,这是我的代码

public class Processor<L extends Location<L>> {
    private final Location<L> location;

    public Processor(Location<L> location) {
        this.location = location;
    }

    public Location<L> location() {
        return location;
    }

    ...
}

现在,我想按顺序跳到索引3(这里是一些i索引)。这是我的“思考”。

    with open('data.csv', 'r') as csvfile:
    reader = csv.reader(csvfile, delimiter=';')
    #check if field 12 ist empty
    for row in reader:
        if not row[12]:
            #Add the orders
            orders.append(row)

感谢任何想法/帮助!

1 个答案:

答案 0 :(得分:0)

给出:

$ cat file.csv
Sally Whittaker,2018,McCarren House,312,3.75
Belinda Jameson,2017,Cushing House,148,3.52
Jeff Smith,2018,Prescott House,17-D,3.20
Sandy Allen,2019,Oliver House,108,3.48

您可以使用islice来获取所需的特定行:

import csv 
import itertools as it 

with open('/tmp/file.csv') as f:
    reader=csv.reader(f)
    print(next(it.islice(reader, 2,3)))
# ['Jeff Smith', '2018', 'Prescott House', '17-D', '3.20']