[Tutor] Fwd: Re: Python 3.6 Extract Floating Point Data from a Text File
Alan Gauld
alan.gauld at yahoo.co.uk
Sun Apr 30 14:47:14 EDT 2017
How, embarrassing, I forgot to CC the list! :-)
On 30/04/17 11:09, Stephen P. Molnar wrote:
> I would have managed to extract input data from another calculation (not
> a Python program) into the following text file.
>
> LOEWDIN ATOMIC CHARGES
> ----------------------
> 0 C : -0.780631
> 1 H : 0.114577
>
> What I need to do is extract the floating point numbers into a Python file
>From previous posts we know you can extract a line of text so really you
are asking how to extract a floating point number from a string.
>>> s = ' 0 C : -0.780631'
>>> fps = s.split()[-1] # get the last item from the split string
>>> val = float(fps)
>>> print( val )
Or in one line:
val = float( line.split()[-1] )
Obviously if the number were not at the end you would have to use
the appropriate index instead of -1...
--
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