You can use literal_eval in the ast module
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
Answer from Padraic Cunningham on Stack OverflowYou can use literal_eval in the ast module
>>> from ast import literal_eval
>>> s = "['a',['b','c','d'],'e']"
>>> print(literal_eval(s))
['a', ['b', 'c', 'd'], 'e']
If you can convert those single quotes to double quotes, you can use json parsing.
import json
obj1 = json.loads('["a", ["b", "c", "d"], "e"]')
For the normal arrays, use ast.literal_eval:
>>> from ast import literal_eval
>>> x = "[1,2,3,4]"
>>> literal_eval(x)
[1, 2, 3, 4]
>>> type(literal_eval(x))
<type 'list'>
>>>
numpy.array's though are a little tricky because of how Python renders them as strings:
>>> import numpy as np
>>> x = [[1,2,3], [4,5,6]]
>>> x = np.array(x)
>>> x
array([[1, 2, 3],
[4, 5, 6]])
>>> x = str(x)
>>> x
'[[1 2 3]\n [4 5 6]]'
>>>
One hack you could use though for simple ones is replacing the whitespace with commas using re.sub:
>>> import re
>>> x = re.sub("\s+", ",", x)
>>> x
'[[1,2,3],[4,5,6]]'
>>>
Then, you can use ast.literal_eval and turn it back into a numpy.array:
>>> x = literal_eval(x)
>>> np.array(x)
array([[1, 2, 3],
[4, 5, 6]])
>>>
Python 2.7.5+ (default, Sep 19 2013, 13:48:49)
>>> import json
>>> json.loads("[1,2,3,4]")
[1, 2, 3, 4]
>>>
There's always everybody's old favourite ast.literal_eval
>>> import ast
>>> x = "[1,'Hello',['test','3'],True,2.532]"
>>> y = ast.literal_eval(x)
>>> y
[1, 'Hello', ['test', '3'], True, 2.532]
>>> z = str(y)
>>> z
"[1, 'Hello', ['test', '3'], True, 2.532]"
ast.literal_eval is better. Just to mention, this is also a way.
a=[[1,2],[2,3],[3,4]]
string_list = str(a)
original_list = eval(string_list)
print original_list == a
# True
Use int which converts a string to an int, inside a list comprehension, like this:
desired_array = [int(numeric_string) for numeric_string in current_array]
List comprehensions are the way to go (see @sepp2k's answer). Possible alternative with map:
list(map(int, ['1','-1','1']))
import array as arr
output = arr.array('b', [ord(c) for c in 'abcdef'])
will output
array('b', [97, 98, 100, 101, 102])
Of course, you have to remember to convert back to characters with chr(), whenever you need to use them as letters/strings.
My answer is limited to NumPy array. Try just this:
import numpy as np
array = np.array(list("acb"))
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
For 1D arrays, Numpy has a function called fromstring, so it can be done very efficiently without extra libraries.
Briefly you can parse your string like this:
s = '[0 1 2 3]'
a = np.fromstring(s[1:-1], dtype=np.int, sep=' ')
print(a) # [0 1 2 3]
For nD arrays, one can use .replace() to remove the brackets and .reshape() to reshape to desired shape, or use Merlin's solution.
Try this:
xs = '[0 1 2 3]'
import re, ast
ls = re.sub('\s+', ',', xs)
a = np.array(ast.literal_eval(ls))
a # -> array([0, 1, 2, 3])