How can I read only the header column of a CSV file using Python?

后端 未结 9 711
生来不讨喜
生来不讨喜 2020-12-14 10:05

I am looking for a a way to read just the header row of a large number of large CSV files.

Using Pandas, I have this method available, for each csv file:

         


        
9条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 10:26

    it depends on what the header will be used for, if you needed the headers for comparison purposes only (my case) this code will be simple and super fast, it will read the whole header as one string. you can transform all the collected strings together according to your needs:

    for filename in glob.glob(files_path+"\*.csv"):
        with open(filename) as f:
            first_line = f.readline()
    

提交回复
热议问题