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
    
@Öskå Are you able to answer this question here? I am not sure whether it is related: I have header with four lines where unique identifier is specified by two lines. –  hhh Nov 11 at 3:17

3 Answers 3

up vote 9 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
    
@george2079, does your apprach require ImportString or, in any case, can it be adapted to import CSV files, skipping blocks of rows (of varying length) consisting entirely of NullWords? –  alancalvitti Nov 11 at 2:31
    
Are you able to answer this question here? I am not sure whether it is related: I have header with four lines where unique identifier is specified by two lines. –  hhh Nov 11 at 3:16

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.

share|improve this answer
    
I find this far more readable than george2079, upvote it! +1. Can someone explain why the other answer has got so many upvotes and this only 2? –  hhh Nov 11 at 3:58

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

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.