CSV read specific row

后端 未结 5 778
鱼传尺愫
鱼传尺愫 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 08:00

    Use list to grab all the rows at once as a list. Then access your target rows by their index/offset in the list. For example:

    #!/usr/bin/env python
    
    import csv
    
    with open('source.csv') as csv_file:
        csv_reader = csv.reader(csv_file)
        rows = list(csv_reader)
    
        print(rows[8])
        print(rows[22])
    

提交回复
热议问题