pythonnumpyabstract-syntax-tree

Changing the format of data in Python


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]]])]

Solution

  • From the docs of ast.literal_eval:

    Evaluate an expression node or a string containing only a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None and Ellipsis.

    Which means that you cannot use literal_eval to parse non-native types such as numpy.array.

    Some suggestions how you can solve this: