2
$\begingroup$

I have a complicated data set (see the image below), and I need to read the magnitude of Etot from this data set. Any idea how can I do that?

file

$\endgroup$
2

2 Answers 2

2
$\begingroup$

Import the data into Mathematica as text, line-by-line:

raw = Import["https://pastebin.com/raw/nhnDApae", {"Text", "Lines"}];

Select the lines that contain Etot:

sl = Select[raw, StringContainsQ[#, "Etot"] &];

Finally, use StringCases[] with NumberString to extract the numerical data, and convert them with ToExpression[]:

etotList = ToExpression[First[StringCases[#, NumberString]], InputForm] & /@ sl
   {-76137.07, -76237.9692, -76166.3972, -76178.2419, -75963.3368}

If you are okay with undocumented functions:

etotList = Internal`StringToDouble[First[StringCases[#, NumberString]]] & /@ sl
$\endgroup$
1
  • 3
    $\begingroup$ Note that InternalStringToDouble` does not return any error if it fails, but simply 0., which might be a pitfall. $\endgroup$
    – Theo Tiger
    Oct 4, 2018 at 19:58
1
$\begingroup$
data = Import["https://pastebin.com/raw/nhnDApae", {"Text", "Lines"}];

Join @@ StringCases[data, "Etot   =" ~~ a__ ~~ "EKtot"  :> ToExpression@ a]

{-76137.07, -76237.9692, -76166.3972, -76178.2419, -75963.3368}

$\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.