0

I have this string:

'[[57.7322273254, -58.8288497925, -54.460193634, 28.4842605591, -45.9620323181, -13.6266260147, -17.2981243134, -15.1332969666, -15.1287126541, -2.44765377045, -0.488036692142, -9.05566310883, -4.70651531219], [72.5999526978, -83.4902877808, -16.4493045807, 40.4356307983, -33.9553756714, -10.7394323349, -17.31067276, -15.6521835327, -25.1421508789, -13.1496963501, -4.11457395554, -14.9144859314, -5.76139545441]]'

I would like to convert this string into a numpy array of arrays

Justin Lange
  • 897
  • 10
  • 25

1 Answers1

3
import ast

print(ast.literal_eval('[[57.7322273254, -58.8288497925, -54.460193634, 28.4842605591, -45.9620323181, -13.6266260147, -17.2981243134, -15.1332969666, -15.1287126541, -2.44765377045, -0.488036692142, -9.05566310883, -4.70651531219], [72.5999526978, -83.4902877808, -16.4493045807, 40.4356307983, -33.9553756714, -10.7394323349, -17.31067276, -15.6521835327, -25.1421508789, -13.1496963501, -4.11457395554, -14.9144859314, -5.76139545441]]'))

This will make this string into a list.

You can cast it with np.array in order to have it as a numpy array.

Read here about ast.literal_eval

Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

omri_saadon
  • 10,193
  • 7
  • 33
  • 58