Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

When importing a data file what are the comment symbols for Mathematica? That is, given a file like this

blabla
bulbul

1 2 6 54 7 ...
..

what symbol do I have to put in front of header lines so Mathematica skips them and starts reading at the line 1 2 6 54 7 .... I tried #, which works in gnuplot, but that did not work.

I know that I could just tell Mathematica to skip the lines, but as I can control the file output, it would be nicer to use some kind of a tag.

share|improve this question
3  
The simplest way is to Import[something][[n+1;;]] where n is the number of "commented" line you have. –  Öskå Jun 13 at 12:18
    
possible duplicate of ReadList with header and foot lines –  rcollyer Jun 13 at 12:40
    
At first glance, I thought this was a duplicate. It is not. The difference is that the number of header lines may be different each time, so it requires processing. –  rcollyer Jun 13 at 12:41

3 Answers 3

up vote 8 down vote accepted

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

share|improve this answer
    
Strictly speaking your last example doesn't handle in-line comments, but only handles comments that are terminated by a line ending. In-line comments need a beginning and an end delimiter. –  m_goldberg Jun 13 at 15:48
    
ok.. not sure what to call that but I'll fix.. –  george2079 Jun 13 at 16:45

Here's one possibility. I made a CSV file that imports looking like this:

data = Import["temp.csv"]
(* {{"header 1", ""}, {"header 2", ""}, {"#", ""}, {1, 1}, {2, 4}, {3, 9}, {4, 16}} *)

Search for your flag (I used #) using Position and then select all rows following that search

data[[Position[data, "#"][[1, 1]] + 1 ;;]]
(* {{1, 1}, {2, 4}, {3, 9}, {4, 16}} *)
share|improve this answer

I like to just import it and then filter it afterwards.

data = Import["file", "Table"];
data = Cases[data, {_?NumberQ, _}];

which will contain only those lines that start with a number.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.