Q:

Convert string array that contains '[' , ']' to int array

I'm attempting to convert an array of strings to array of floats using : 
arr_str = '[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]'

a1 = arr_str.split()

[int(x) for x in a1]

but throws error : 
<
ipython-input-57-f7f1eaba7ebd> in <listcomp>(.0)
      3 a1 = arr_str.split()
      4 
----> 5 [int(x) for x in a1]
      6 
      7 # for a in arr_str.split():

ValueError: invalid literal for int() with base 10: '[1'

Should pre-process the string and remove '[' and ']' ?

A:

One way is to use ast.literal_eval.
If you need a numpy integer array, the conversion is trivial.
import numpy as np
from ast import literal_eval

arr_str = '[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]'

res = literal_eval(arr_str.replace(' ', ','))

# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

res_np = np.array(res)

# array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

