从CSV文件读取特定行

mar*_*t1n 0 python csv

我有一个十行的CSV文件。从这个文件中,我只想要第四行。最快的方法是什么?我正在寻找类似的东西:

with open(file, 'r') as my_file:
    reader = csv.reader(my_file)
    print reader[3]
Run Code Online (Sandbox Code Playgroud)

reader[3]对于我要实现的语法,哪里显然是不正确的语法。如何将阅读器移至第4行并获取其内容?

Mar*_*ers 5

如果只有10行,则可以将整个文件加载到列表中:

with open(file, 'r') as my_file:
    reader = csv.reader(my_file)
    rows = list(reader)
    print rows[3]
Run Code Online (Sandbox Code Playgroud)

对于较大的文件,请使用itertools.islice()

from itertools import islice

with open(file, 'r') as my_file:
    reader = csv.reader(my_file)
    print next(islice(reader, 3, 4))
Run Code Online (Sandbox Code Playgroud)