1
$\begingroup$

I have the following data file which contains four columns of data

However when I import it as

data = Import["Exo_data.txt", "Table"];

I get the following list

data = {{11, "Com", "b", 19.4, 0.231, 2.7}, {11, "Oph", "b", 21, 0.0162},
        {11, "UMi", "b", 10.5, 0.08, 1.8}, {14, "And", "b", 5.33, 0, 2.2}, ...}

The first column of the data file contains the names, e.g., 11 Com b, however when I Import it the name is split into multiple columns. I want to obtain the following list

data = {{"11 Com b", 19.4, 0.231, 2.7}, {"11 Oph b", 21, 0.0162},
        {"11 UMi b", 10.5, 0.08, 1.8}, {"14 And b", 5.33, 0, 2.2}, ...}

Any suggestions?

$\endgroup$
3
  • $\begingroup$ Does ReadList["Exo_data.txt"] work? $\endgroup$
    – user49047
    Sep 4, 2019 at 12:53
  • $\begingroup$ or data = Import["Exo_data.txt", "List"] $\endgroup$
    – user49047
    Sep 4, 2019 at 13:08
  • $\begingroup$ @user49047 None of them work correctly. $\endgroup$
    – Vaggelis_Z
    Sep 4, 2019 at 13:09

2 Answers 2

4
$\begingroup$

Here's a solution using the "FieldSeparators" option for the "Table" import format:

Import[
 "exo_data.txt",
 "Table",
 "FieldSeparators" -> {"\t"},
 "RepeatedSeparators" -> False,
 HeaderLines -> 1
 ]

(* {
     {"11 Com b", 19.4, 0.231, 2.7},
     {"11 Oph b", 21, "", 0.0162},
     …
   } *)

Note the use of "RepeatedSeparators"->False to preserve the empty entries in some of the rows.

If you want to preserve the headers, you could do the following:

Dataset@
 Query[Transpose]@
  (AssociationThread[First@# -> Transpose@Rest@#] &)@
   Import[
     "exo_data.txt",
     "Table",
     "FieldSeparators" -> {"\t"},
     "RepeatedSeparators" -> False
     ][[;; -5]]

enter image description here

$\endgroup$
3
$\begingroup$

It seems that using SemanticImport with tab as a delimiter does what you want:

SemanticImport["~/Downloads/Exo_data.txt", Delimiters -> "\t"]

enter image description here

$\endgroup$

Your Answer

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

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