Skip to content Skip to sidebar Skip to footer

Read Pandas Dataframe From Csv Beginning With Non-fix Header

I have a number of data files produced by some rather hackish script used in my lab. The script is quite entertaining in that the number of lines it appends before the header varie

Solution 1:

You can open file and iterate it until consecutive \r\n are met, and pass result to parser, i.e.

with open(csv_file_name, 'rb') as source:
    consec_empty_lines = 0
    for line in source:
        if line == '\r\n':
            consec_empty_lines += 1
            if consec_empty_lines == 2: 
                break
        else:
            consec_empty_lines = 0
    df = pd.read_csv(source)

Post a Comment for "Read Pandas Dataframe From Csv Beginning With Non-fix Header"