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?
-
$\begingroup$ Please put the contents of the file in Pastebin. $\endgroup$– J. M.'s eventual burnout ♦Oct 4, 2018 at 15:00
-
$\begingroup$ it is done pastebin.com/nhnDApae $\endgroup$– ParvizOct 4, 2018 at 15:51
2 Answers
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
-
3$\begingroup$ Note that
Internal
StringToDouble` does not return any error if it fails, but simply 0., which might be a pitfall. $\endgroup$ Oct 4, 2018 at 19:58
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}