3
$\begingroup$

Consider the following file. It has the structure of a table, with rows like

LHC HNL dipole  0.5 0.00001 e   {"HNL dipole", 0.5, 0.00001, 0.}

Here, the columns are LHC, HNL dipole, 0.5, 0.00001, e, {...}.

I fail to import it. Namely, if importing in the following way,

data = Import[FileNameJoin[{NotebookDirectory[], 
    "Efficiencies.dat"}], "Table"];

I get

data[[1]]

{"LHC", "HNL", "dipole", 0.5, 0.00001, "e", "{"HNL dipole",","0.5,", "0.00001,", "0.}"}

I.e., "HNL dipole" is separated as "HNL" and "dipole", while the data in the sub-row is completely unreadable:

data[[1]][[7]]

"{"HNL dipole","

Could you please tell me how to import the file properly?

$\endgroup$
1
  • 1
    $\begingroup$ d2 = Import["C:/Efficiencies.dat", "TSV"]. Now if this works, then please say how you would like to manipulate this data further. Thanks. $\endgroup$
    – Syed
    Dec 23, 2022 at 9:47

2 Answers 2

4
$\begingroup$
ReadList["~/Downloads/Efficiencies.dat", "Word",
  RecordLists -> True, WordSeparators -> {",", "\t", " ", "{", "}"}]

(*    {{"LHC", "HNL", "dipole", "0.5", "0.00001", "e", "HNL", "dipole", "0.5", "0.00001", "0."},
       {"LHC", "HNL", "dipole", "0.5", "0.00001", "μ", "HNL", "dipole", "0.5", "0.00001", "0."},
       ...
       {"FCC-hh", "HNL", "dipole", "7.", "19.952623149688787", "μ", "HNL", "dipole", "7.", "19.952623149688787", "0.000024379781172545756"}}    *)

If you don't like the "μ" parsing, you can do % /. "μ" -> "\[Mu]". Also, ToExpression converts the strings to numbers, if desired.

$\endgroup$
6
$\begingroup$

Each line of the file contains 6 tab separated fields, so Import should specify the TSV format. The last field can be trimmed to remove the braces and then parsed using ImportString. For example

data = With[{raw = Import["Efficiencies.dat", "TSV"]},
         raw /. {x___, y_String} :> {x, Flatten@
           ImportString[StringTrim[y, {"{", "}"}], "CSV"]}
   ];

data//First//Last   (* {"HNL dipole", 0.5, 0.00001, 0.}}  *)
$\endgroup$

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.