Is there a function that can strip the apostrophe from the data which has been taken from Excel?

Tags: , ,



I’m extracting data from Excel as a multidimensional array. When I try to loop through the array in order to obtain each value, few values in the obtained list contain apostrophes and few other values don’t. Is there any way to rectify this?

I tried using strip function, however it was not fruitful. Please help.

I expect the output to be (2.56942078E+00, -8.59741137E-05, 4.19484589e-08, -1.00177799e-11, 1.22833691e-15, 29217.5791, 4.78433864)

But I end up with ('2.56942078E+00', '-8.59741137E-05', 4.19484589e-08, -1.00177799e-11, 1.22833691e-15, 29217.5791, 4.78433864)

Answer

The apostrophes show that the item is a string instead of a float. The ' is just a representation of the string. To use it as a float, just convert the type.

array = ('2.56942078E+00', '-8.59741137E-05', 4.19484589e-08, -1.00177799e-11, 1.22833691e-15, 29217.5791, 4.78433864)

for item in array:
    print(type(item))

<class 'str'>
<class 'str'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>

See how the first two items are strings instead of floats. Now just convert the item to float before doing something else to it:

for item in array:
    item = float(item)
    print(type(item))

<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>


Source: stackoverflow