# Importing a ragged array while ignoring rows that start with “#” [duplicate]

I am new to Mathematica, and I find that there may be a lot of ways of doing this. I was wondering which one might be the most general.

I am importing data from .txt files, resulting in a ragged array due to many lines containing parameters and other comments which I want to ignore. I just want to import the numbers, and ignore the comments.

I am using this:

Import["filename.txt", "TSV", "HeaderLines" -> 15]


I have no TrailerLines that I need to skip, however, there are many rows halfway through the data which I would like to remove. These lines all start with #, which makes me think that there is a way to just specify that any line starting with # be skipped.

Thanks a lot in advance to anyone who might be able to help me with this.

-
Duplicate: 21075. Related: 2655, 29384 –  Michael E2 Aug 20 '13 at 14:35
I'd be tempted to use DefineImportStreamMethod to do this, but it's use becomes more complex: you have to open the stream first with the method, and then use Import. But, Import's ability to use streams is undocumented, so your mileage may vary. –  rcollyer Aug 20 '13 at 14:38
Oh. Sorry about the duplicate question. I was not searching correctly I think. And thanks a lot to all for the help. –  CSR Aug 20 '13 at 14:44
@CSR I didn't find it at first, either. :) –  Michael E2 Aug 20 '13 at 18:30

## marked as duplicate by Michael E2, Sjoerd C. de Vries, rm -rf♦Aug 21 '13 at 0:44

You could delete them after Import.

DeleteCases[Import["filename.txt", "Data", "HeaderLines" -> 10], {_String, ___}]


or more precisely

DeleteCases[Import["filename.txt", "Data", "HeaderLines" -> 10],
{_String?(StringMatchQ[#, "#*"] &), ___}]

-

I think the easy way is to import the data and removed the comments afterward.

data = "1\t2\t3\t4\n# comment\n42\t43\t44\n# comment\n5\t6";
data = ImportString[data, "TSV"]

{{1, 2, 3, 4}, {"# comment"}, {42, 43, 44}, {"# comment"}, {5, 6}}


I used ImportString to make Mathematica internally generate a simulated TSV file for the purposes of this answer. Your Import expression should work fine for you.

data = DeleteCases[data, {_String}]

 {{1, 2, 3, 4}, {42, 43, 44}, {5, 6}}

-