String formatting, scientific notation to float in Python -


this question has answer here:

i using python pretty new , guidance believe string formatting problem.

i have output external program want "translate" floats. output contains 2 numbers may or may not in scientific notation , can have 15 digits after decimal point (which have omitted here).

(-1.040e-05+3.1676e-03j) want become 2 separate numbers -0.00001040 , 0.0031676

other examples of output data in useless form follows (0.0572636-0.419420j) become 0.0572636 , -0.419420 (0.000194+4.85091e-05j)

i aim take 2 numbers contained in each pair of brackets, square them , add them together. if result 2 number in scientific notation fine, long able perform math operations on them. minus sign doesn't need survive transformation disappear when square anyway. if makes things easier.

the approach taking rather convoluted , messy method. using x.find locate 'e', '-' , '+' , interpreting parts of string should extracted form number.

i unfamiliar re module , unsure how use extract right format.

any apprieciated

you can use excellent numpy package:

import numpy np = np.array(-1.040e-05+3.1676e-03j) i.real  >> array(-1.04e-05)  i.imag  >> array(0.0031676) 

Comments