How to check if a CSV has a header using Python?

前端 未结 5 583
再見小時候
再見小時候 2021-01-17 22:54

I have a CSV file and I want to check if the first row has only strings in it (ie a header). I\'m trying to avoid using any extras like pandas etc. I\'m thinking I\'ll use a

5条回答
  •  独厮守ぢ
    2021-01-17 23:30

    Python has a built in CSV module that could help. E.g.

    import csv
    with open('example.csv', 'rb') as csvfile:
        sniffer = csv.Sniffer()
        has_header = sniffer.has_header(csvfile.read(2048))
        csvfile.seek(0)
        # ...
    

提交回复
热议问题