Autodetect Presence of CSV Headers in a File

后端 未结 6 1539
心在旅途
心在旅途 2020-12-25 13:23

Short question: How do I automatically detect whether a CSV file has headers in the first row?

Details: I\'ve written a small CSV parsing engine th

6条回答
  •  不思量自难忘°
    2020-12-25 13:31

    As others have pointed out, you can't do this with 100% reliability. There are cases where getting it 'mostly right' is useful, however - for example, spreadsheet tools with CSV import functionality often try to figure this out on their own. Here's a few heuristics that would tend to indicate the first line isn't a header:

    • The first row has columns that are not strings or are empty
    • The first row's columns are not all unique
    • The first row appears to contain dates or other common data formats (eg, xx-xx-xx)

提交回复
热议问题