I have string A. I want to convert into an array but I am facing an error. I present the current and expected outputs.
import ast
import numpy as np
A = '\n[array([[[ 0, 22],\n [ 0, 23]]], dtype=int64)]'
# Remove unnecessary characters and fix dtype
A_cleaned = A.replace('\n', '').replace('dtype=int64', '').strip()
# Extract the content inside the outermost brackets
start_idx = A_cleaned.find('[')
end_idx = A_cleaned.rfind(']')
inner_list_str = A_cleaned[start_idx:end_idx+1]
# Replace 'array' with 'np.array'
inner_list_str = inner_list_str.replace('array', 'np.array')
# Use ast.literal_eval to safely evaluate the string as a Python literal
try:
inner_list = ast.literal_eval(inner_list_str)
np_array = np.array(inner_list)
print("NumPy array:")
print(np_array)
print("Data type of np_array:", type(np_array))
except Exception as e:
print(f"Error: Unable to convert the string to a NumPy array. {e}")
The current output is
Error: Unable to convert the string to a NumPy array. malformed node or string on line 1: <ast.Call object at 0x000001709540B640>
The expected output is
[array([[[ 0, 22], [ 0, 23]]])]
From the docs of
ast.literal_eval:Which means that you cannot use
literal_evalto parse non-native types such asnumpy.array.Some suggestions how you can solve this:
evalinstead ofast.literal_eval(WARNING be careful as this might be harmful since string will be evaluated as python-code without any checks!!)
np.arraypart as well and parse the data as alistwithast.literal_evaland then convert it to a numpy-array.