Venkata Narsi Reddy Vaddula Venkata Narsi Reddy Vaddula - 11 months ago 52
Python Question

Python: execute for loop only once(not the first time but second time)?

fileInput consists of a csv file with some 30 rows and I want to access only the second row.

for row in csv.reader(fileInput):

I need to access the second row not the first one(first one contains col names).
gives second column in

only the second row not from the second row!

Answer Source

You might want to consider using DictReader. It'll handle the header for you and allow you to access the columns by name rather than index (which can help with keeping code readable).


import csv

with open('names.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    row = next(reader)
    print(row['first_name'], row['last_name'])