Home > OS >  Converting string array into float array
Converting string array into float array

Time:12-11

I have multiple points that look like this:

points = '[1078.17,436.18],[1089.48,413.57],[1092.71,389.35],[1091.09,365.12],[1089.48,337.67],[1073.32,316.67],[1057.17,295.68],[1036.18,282.75],[1011.95,279.52],[987.73,273.06],[961.89,273.06],[937.66,276.29],[913.43,281.14],[894.05,297.29],[880.60,316.70],[874.20,343.10],[871.44,371.58],[868.21,395.81],[868.21,421.65],[887.59,437.80],[911.82,444.26],[936.04,449.11],[960.27,452.34],[984.50,453.95],[1010.34,457.18],[1034.56,455.57],[1058.79,447.49]' 

the points are string but I'm trying to convert it to float so it'll look like this:

points = [1078.17,436.18],[1089.48,413.57],[1092.71,389.35],[1091.09,365.12],[1089.48,337.67],[1073.32,316.67],[1057.17,295.68],[1036.18,282.75],[1011.95,279.52],[987.73,273.06],[961.89,273.06],[937.66,276.29],[913.43,281.14],[894.05,297.29],[880.60,316.70],[874.20,343.10],[871.44,371.58],[868.21,395.81],[868.21,421.65],[887.59,437.80],[911.82,444.26],[936.04,449.11],[960.27,452.34],[984.50,453.95],[1010.34,457.18],[1034.56,455.57],[1058.79,447.49]

or:

points = [[1078.17,436.18],[1089.48,413.57],[1092.71,389.35],[1091.09,365.12],[1089.48,337.67],[1073.32,316.67],[1057.17,295.68],[1036.18,282.75],[1011.95,279.52],[987.73,273.06],[961.89,273.06],[937.66,276.29],[913.43,281.14],[894.05,297.29],[880.60,316.70],[874.20,343.10],[871.44,371.58],[868.21,395.81],[868.21,421.65],[887.59,437.80],[911.82,444.26],[936.04,449.11],[960.27,452.34],[984.50,453.95],[1010.34,457.18],[1034.56,455.57],[1058.79,447.49]]

where the shape should be in this case 27x2

I tried np.float and np.astype but non seems to work. The error I got with np.float is:

<ipython-input-146-cdfdb0cec2ea>:1: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  np.float(test)

I also tried float by itself:

float(points)

got the following error

ValueError: could not convert string to float: '[1078.17,436.18],[1089.48,413.57],[1092.71,389.35],[1091.09,365.12],[1089.48,337.67],[1073.32,316.67],[1057.17,295.68],[1036.18,282.75],[1011.95,279.52],[987.73,273.06],[961.89,273.06],[937.66,276.29],[913.43,281.14],[894.05,297.29],[880.60,316.70],[874.20,343.10],[871.44,371.58],[868.21,395.81],[868.21,421.65],[887.59,437.80],[911.82,444.26],[936.04,449.11],[960.27,452.34],[984.50,453.95],[1010.34,457.18],[1034.56,455.57],[1058.79,447.49]'

Can someone help me converting the string into float array?

CodePudding user response:

Try with ast module

import ast
points = ast.literal_eval(points)

CodePudding user response:

IF the the input for your points string doesn't come from a user you can just use:

result = eval(points)

This will return a tuple of lists of floats.

Please be aware to never use eval with user input.

  • Related