[Tutor] Extract element of dtype object
Mats Wichmann
mats at wichmann.us
Sun Dec 6 11:28:27 EST 2020
On 12/6/20 6:37 AM, Stephen P. Molnar wrote:
> I have a large number of files from which I wish to extract a number.
Your succession of examples is not leaving me, at least, with any
clarity of what you're actually looking for. You need to define for
yourself a description in words, then you can probably begin to code it
up. Youi include "and the code snippet" but there is no code snippet...
see below
>
> This is an example or a portion of the file:
>
> mode | affinity | dist from best mode
> | (kcal/mol) | rmsd l.b.| rmsd u.b.
> -----+------------+----------+----------
> 1 -8.538750714 0.000 0.000
> 2 -8.487003690 3.712 7.079
> 3 -8.420436250 3.018 6.604
> 4 -7.992952187 3.711 7.282
> 5 -7.933139639 2.128 6.739
> 6 -7.889517329 2.667 6.537
> 7 -7.720785374 20.143 23.808
> 8 -7.629772565 21.218 25.083
> 9 -7.560349273 2.136 7.865
> Writing output ... done.
>
> and the code snippet:
>
> Refining results ... done.
>
> 0 mode | affinity | dist from best mode
>
> 1 | (kcal/mol) | rmsd l.b.| rmsd u.b.
>
> 2 -----+------------+----------+----------
>
> 3 1 -8.538750714 0.000 0.000
>
> 4 2 -8.487003690 3.712 7.079
>
> the output:
>
> Refining results ... done. 1 -8.538750714 0.000 0.000
>
>
> Refining results ... done.
> 0 mode | affinity | dist from best mode
> 1 | (kcal/mol) | rmsd l.b.| rmsd u.b.
> 2 -----+------------+----------+----------
> 3 1 0.000 0.000
> 4 2 -8.487003690 3.712 7.079
> Refining results ... done. 1 -8.538750714 0.000 0.000
> Name: 3, dtype: object
>
> The number that I want to extract is -8.538750714.
If you're collecting lines from running an external script, you can run
that script with subprocess.run and then look through the resulting
output until you find a line that matches some recognizable pattern.
That pattern seems to be "Refining results ... done." but it appears so
many times in the above it's hard to sort out what's what. If that's the
case you can then trim that line either with plain string handling
routines or with a regular expression (regexes get a bad reputation,
they're good for handling text with recognizable patterns, but may be
overkill for this case)
Will wait for some clarification before digging further...
More information about the Tutor
mailing list