Skipping empty csv rows

cheatsheet
pandas
Author

Teresa Kubacka

Published

August 4, 2022

Let’s say you got a series of csv files which contain tabular data, but there is a certain amount of commentary above the table:

Comment line 1 
Comment line 2 
Comment line 3 

col1, col2, col3 
10, 12, 13
21, 20, 22

Pandas accepts also file objects as an input, so you can travel through the lines until you find a separating line (or another condition) and then proceed with pd.read_csv:

import pandas as pd

f = open("data.csv")
while f.readline() != '\n':
    pass

df = pd.read_csv(f, header=None)
f.close()

As suggested in src