2
$\begingroup$

I have a text file created with a bash script I wanted to read the data avoiding the first line.

I have written few dirty codes, but I wondered if anybody can do it in fewest possible nice one liners. I'm trying to learn to code more efficiently.

Here is the table.

Compound        State       Method      Approach    S^2         Energy              Cube file       Path
CF2             triplet     CC          NONE        2.007614    -237.329191671337   plotted         ./3B1/CF2-CC-t.out
CF2             triplet     DFT         NONE        2.005408    -237.601091985298   plotted         ./3B1/CF2-t.out

I am looking for the nicest possible way to read all this table into a matrix DATA[[x,y]]

$\endgroup$
3
  • 2
    $\begingroup$ Look up Import of TSV files paying attention to the "TextDelimiters" option. $\endgroup$
    – Edmund
    Commented Sep 5, 2015 at 12:54
  • $\begingroup$ It may also be useful to you to add "HeaderLines" -> 1 to your Import expression to avoid importing the first line. Alternatively you could use a construct like Import[your arguments][[2;;]] to discard that first line. $\endgroup$
    – MarcoB
    Commented Sep 5, 2015 at 13:27
  • $\begingroup$ Thanks for the accept, but would be better for you and the community if you were to remove the accept and merely up-vote my answer. It is good idea not to accept answers for 24 to 48 hours after posting so as to encourage alternative answers. At the end of the 24 - 48 hour period, you should then accept the best answer. $\endgroup$
    – m_goldberg
    Commented Sep 5, 2015 at 14:24

1 Answer 1

3
$\begingroup$

Assuming the file containing the data has the extension ".tsv" and that you want to choose the file from a system file-open dialog (rather than type in the file name), it can be done with

data = 
   Module[{path}, 
     path = 
       SystemDialogInput["FileOpen", "*.tsv", WindowTitle -> "Select a TSV file"];
     If[path === $Canceled, $Canceled, Import[path, "HeaderLines" -> 1]]];
data
{{"CF2", "triplet", "CC", "NONE", 2.00761, -237.329, "plotted", "./3B1/CF2-CC-t.out"}, 
 {"CF2", "triplet", "DFT", "NONE", 2.00541, -237.601, "plotted", "./3B1/CF2-t.out"}}

If you don't want to be restricted to the extension ".tsv", use

data = 
   Module[{path}, 
     path = 
       SystemDialogInput["FileOpen", "*.*", WindowTitle -> "Select a data file"];
     If[path === $Canceled, $Canceled, Import[path, "TSV", "HeaderLines" -> 1]]];

But the file itself will still have to be a tab delimited file.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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