CSV read specific row

后端 未结 5 785
鱼传尺愫
鱼传尺愫 2020-12-03 07:27

I have a CSV file with 100 rows.

How do I read specific rows?

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

5条回答
  •  悲哀的现实
    2020-12-03 07:55

    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
    

提交回复
热议问题