Autodetect Presence of CSV Headers in a File

Posted by banzaimonkey on Stack Overflow See other posts from Stack Overflow or by banzaimonkey
Published on 2010-04-19T20:14:57Z Indexed on 2010/04/19 20:33 UTC
Read the original article Hit count: 402

Filed under:
|
|
|

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 that places the data into an object that I can access as (approximately) an in-memory database. The original code was written to parse third-party CSV with a predictable format, but I'd like to be able to use this code more generally.

I'm trying to figure out a reliable way to automatically detect the presence of CSV headers, so the script can decide whether to use the first row of the CSV file as keys / column names or start parsing data immediately. Since all I need is a boolean test, I could easily specify an argument after inspecting the CSV file myself, but I'd rather not have to (go go automation).

I imagine I'd have to parse the first 3 to ? rows of the CSV file and look for a pattern of some sort to compare against the headers. I'm having nightmares of three particularly bad cases in which:

  1. The headers include numeric data for some reason
  2. The first few rows (or large portions of the CSV) are null
  3. There headers and data look too similar to tell them apart

If I can get a "best guess" and have the parser fail with an error or spit out a warning if it can't decide, that's OK. If this is something that's going to be tremendously expensive in terms of time or computation (and take more time than it's supposed to save me) I'll happily scrap the idea and go back to working on "important things".

I'm working with PHP, but this strikes me as more of an algorithmic / computational question than something that's implementation-specific. If there's a simple algorithm I can use, great. If you can point me to some relevant theory / discussion, that'd be great, too. If there's a giant library that does natural language processing or 300 different kinds of parsing, I'm not interested.

© Stack Overflow or respective owner

Related posts about php

Related posts about csv