Skip header lines on import
Here is an approach that handles interspersed comments in addition to "headers"
FilePrint["test.txt"]
#comment #comment #comment 1 2 3 #c2 4 5 6 7 8 9
ImportString[
StringReplace[Import["test.txt", "Text"],
StartOfLine ~~ "#" ~~ Shortest[___] ~~ EndOfLine ~~ "\n" -> ""], "Table"]
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
of course you can invent whatever convention you want or even a mix, eg..
`StartOfLine ~~ {"#", "!", "%"} ~~ ...`
another variant:
ImportString[StringJoin@Riffle[
Select[StringSplit[Import["test.txt", "Text"], "\n"],
StringTake[#, 1] != "#" &], "\n"], "Table"]
Even handle end-of-line comments:
#comment 1 2 3 #c2 4 5 6 #note 1 7 8 9
ImportString[StringReplace[Import["test.txt", "Text"], {
StartOfLine ~~ "#" ~~ Shortest[___] ~~ EndOfLine ~~ "\n" -> "",
"#" ~~ Shortest[___] ~~ EndOfLine -> ""
}], "Table"]
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Tested on Windows by the way -- this might need some tweaking to handle different line endings on other systems
I like to just import it and then filter it afterwards.
data = Cases[Import["file", "Table"], {_?NumberQ, ___}];
which will contain only those lines that start with a number.
The Import command supports an option to ignore header lines. In many cases this is the easiest solution. For example:
dataStats = Import["C:/data/stats.csv", "CSV", HeaderLines -> 4];