-1

I have strings in the following format in a text file. The following example has two lines.

[5.00186009e-01 3.77071140e-03 3.95659741e+00 1.89513038e+00 5.60401088e+00 7.09885871e-01] 
[0.         0.         3.14860986 2.70311793 6.28318531 1.51787342]

I read it line by line and for each line I am using the following code to extract the numbers in a float list.

param_list = [float(i) for i in re.findall(r"[-+]?(?:\d*\.\d+|\d+)", line)]

It works well for the second line when the floats are not in scientific notation. How can I modify the regex to allow it to handle the scientific notations?

Thanks.

Omar Shehab
  • 872
  • 2
  • 12
  • 24
  • `np.array(..., dtype=float)` can convert these strings - just make sure you strip off all the []. `np.array(line.strip()[1:-1].split()]` does for me. Or without `numpy`, `[float(i) for i in line.strip()[1:-1].split()]`. If you get a `ValueError`, examine the problem string carefully. – hpaulj May 20 '22 at 19:49

0 Answers0