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

后端 未结 9 727
生来不讨喜
生来不讨喜 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:19

    I've used iglob as an example to search for the .csv files, but one way is to use a set, then adjust as necessary, eg:

    import csv
    from glob import iglob
    
    unique_headers = set()
    for filename in iglob('*.csv'):
        with open(filename, 'rb') as fin:
            csvin = csv.reader(fin)
            unique_headers.update(next(csvin, []))
    

提交回复
热议问题