CSV读取特定行 [英] CSV read specific row

查看:68
本文介绍了CSV读取特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个100行的CSV文件.

I have a CSV file with 100 rows.

如何读取特定行?

我想读第9行或第23行,等等吗?

I want to read say the 9th line or the 23rd line etc?

推荐答案

您可以使用list comprehension来过滤文件,如下所示:

You could use a list comprehension to filter the file like so:

with open('file.csv') as fd:
    reader=csv.reader(fd)
    interestingrows=[row for idx, row in enumerate(reader) if idx in (28,62)]
# now interestingrows contains the 28th and the 62th row after the header

这篇关于CSV读取特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆