[Tutor] Extract element of dtype object

Alan Gauld alan.gauld at yahoo.co.uk
Sun Dec 6 16:08:03 EST 2020


On 06/12/2020 13:37, Stephen P. Molnar wrote:
> I have a large number of files from which I wish to extract a number.

Just one number?
Or a field from each line that happens to be a number?

The two scenarios have very different solutions.

If its a field (and you are on a *nix OS) consider using
[g]awk, its designed for that kind of thing

$ gawk '/^[ 0-9]+/ print {$4}' myfile.txt

prints the 4th field on any liner starting with a number

But do you need a key too? - like the line number say?

> This is an example or a portion of the file:
> -----+------------+----------+----------
>     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

> 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

How can you tell that line is the one you want?
There must be some other criteria?
Or is it that particular number you want - in which
case the solution is trivial.

> The number that I want to extract is -8.538750714.

We need a more precise  description of the problem,.
Precisely expressed requirements lead to precisely
expressed solutions. Vague requirements lead to an
array of confusing possibilities.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list