How to skip the header of a csv file in Python? next() can be used to skip the first row of a csv file and continue to iterate through the csv file at the next row. In a csv file, the first line is usually the header, that explains the columns of the file and if the first row is skipped the header is ignored.
# skip header of csv file
# import csv module to use the csv reader
import csv
# open the csv file and read data
with open('data.csv', 'r') as myfile:
data = csv.reader(data)
# skip the first row, which is the header
next(data)
# iterate through rows in the csv file
for row in data:
print(row)
The next() function can be used to skip more than one row. To skip multiple rows, just repeat the next() function or use a range() to skip a specific number of rows.
To parse the header, the DictReader() can be used. To create an empty csv file use the with open operator or pandas.