0

I have a csv file with comments marked by '#'. I want to select only the table part from this and get it into a pandas dataframe. I can just check the '#' marks and the table header and delete them but it will not be dynamic enough. If the csv file is slightly changed it won't work.

Please help me figure out a way to extract only the table part from this csv file.

  • Maybe this question can help you https://stackoverflow.com/questions/60434664/automatically-determine-header-row-when-reading-csv-in-pandas – Andrea Di Iura Dec 10 '21 at 11:49
  • Exactly how are you generating this csv file? I'd look at that and see why it is creating the "metadata" and comment lines. Address it from that end. – EdStevens Dec 10 '21 at 17:46

2 Answers2

0

There is a comment argument if you read in your file, but each line has to start with the appropriate character or your Metadata will not be treated as comment.

import pandas as pd
df = pd.read_csv('path/to/file.csv', sep=';', comment='#')
steTATO
  • 550
  • 4
  • 12
-1

.csv file can't have comment. Then you must delete comment-line manualy. Try start checking from end file, and stop if # in LINE and ';' not in LINE

ganz
  • 132
  • 6