我有A
号线.我想要转换为数组,但我面临错误.我介绍了当前和预期的输出.
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}")
当前输出为
Error: Unable to convert the string to a NumPy array. malformed node or string on line 1: <ast.Call object at 0x000001709540B640>
预期yields 为
[array([[[ 0, 22], [ 0, 23]]])]