Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Convert string (in scientific notation) to float

I'm trying to import a large .csv file containing text and numbers using genfromtxt in numpy. I'm only interested in two columns. I have most of the import sorted out with:

def importfile(root):
    data = root.entry.get()
    atw = np.genfromtxt(data, delimiter=",",
                        skip_header=1,
                        skip_footer=2,
                        autostrip=True,
                        usecols=(25,26),
                        dtype=("|S10"))
    elem = atw[:,0]
    concs = atw[:,1]
        
    print(elem)
    print(concs)

With output for elem and concs respectively:

['Na2O' 'MgO' 'Al2O3' 'SiO2' 'P2O5' 'SO3' 'Cl' 'K2O' 'CaO' 'TiO2' 'Cr2O3'
'MnO' 'FeO' 'NiO' 'Cu2O' 'ZnO' 'Ga2O3' 'SrO' 'Y2O3']

['3.76E+00' '1.31E+01' '1.14E+01' '4.04E+01' '1.24E+00' '5.89E-02'
'2.43E-02' '1.53E+00' '1.49E+01' '2.87E+00' '6.05E-02' '1.96E-01'
'1.17E+01' '3.69E-02' '8.73E-03' '1.39E-02' '1.93E-03' '1.88E-01'
'5.58E-03']

I have tried many different things for converting the concs string into a float, but it doesn't seem to like the fact that the concs are in scientific notation... is there a way to turn the concs values into a float?

like image 915
Dr. Toboggan Avatar asked May 13 '14 16:05

Dr. Toboggan


People also ask

How do you convert a string to a float in Python?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

How do you convert strings to scientific notation in Python?

[format(x, '. 0e') for x in A] works just as well. @vaultah: Both are good solutions!!

How do you convert a string to a float with 2 decimals in Python?

In Python, to print 2 decimal places we will use str. format() with “{:. 2f}” as string and float as a number. Call print and it will print the float with 2 decimal places.

How do I get rid of scientific notation in Python?

Summary: Use the string literal syntax f"{number:. nf}" to suppress the scientific notation of a number to its floating-point representation.


2 Answers

The float function can do this:

>>> float('1.31E+01') 13.1 

or for a list:

>>> map(float, ['3.76E+00', '1.31E+01', '1.14E+01']) [3.76, 13.1, 11.4] 
like image 169
RichieHindle Avatar answered Sep 22 '22 03:09

RichieHindle


 with open( datafile,'r' ) as inData:      for line in inData:           j = list( map( float,   filter( None  , [ x for x in line.strip().split(',') ] )) ) 

Just mentioned generally, as it solves a similar problem that brought me to this page.

like image 31
mist42nz Avatar answered Sep 24 '22 03:09

mist42nz