list may help you do that.
import numpy as np
mystr = "100110"
print np.array(list(mystr))
# ['1' '0' '0' '1' '1' '0']
If you want to get numbers instead of string:
print np.array(list(mystr), dtype=int)
# [1 0 0 1 1 0]
Answer from dragon2fly on Stack Overflowlist may help you do that.
import numpy as np
mystr = "100110"
print np.array(list(mystr))
# ['1' '0' '0' '1' '1' '0']
If you want to get numbers instead of string:
print np.array(list(mystr), dtype=int)
# [1 0 0 1 1 0]
You could read them as ASCII characters then subtract 48 (the ASCII value of 0). This should be the fastest way for large strings.
>>> np.fromstring("100110", np.int8) - 48
array([1, 0, 0, 1, 1, 0], dtype=int8)
Alternatively, you could convert the string to a list of integers first:
>>> np.array(map(int, "100110"))
array([1, 0, 0, 1, 1, 0])
Edit: I did some quick timing and the first method is over 100x faster than converting it to a list first.
vector - Python numpy: Convert string in to numpy array - Stack Overflow
convert string representation of array to numpy array in python - Stack Overflow
python - convert string to 2d numpy array - Stack Overflow
python - Convert string to numpy array of strings - Stack Overflow
You have to split the string by its commas first:
NP.array(v1fColor.split(","), dtype=NP.uint8)
You can do this without using python string methods -- try numpy.fromstring:
>>> numpy.fromstring(v1fColor, dtype='uint8', sep=',')
array([ 2, 4, 14, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 4, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 6, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 9, 0, 0, 0,
2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 6, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 8, 0, 0, 0, 1, 2,
0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 17, 0, 0, 0, 3, 6, 0,
0, 0, 0, 0, 0, 0, 0, 0, 7, 5, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 4, 3, 0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 6, 6, 0, 0, 0, 2, 3], dtype=uint8)
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])
Instead of splitting and filtering, you could use np.fromstring:
>>> np.fromstring(b, sep='\t').reshape(-1, 4)
array([[ 191.25 , 0. , 0. , 1. ],
[ 191.251, 0. , 0. , 1. ],
[ 191.252, 0. , 0. , 1. ]])
This always returns a 1D array so reshaping is necessary.
Alternatively, to avoid reshaping, if you already have a string of bytes (as strings are in Python 2), you could use np.genfromtxt (with the help of the standard library's io module):
>>> import io
>>> np.genfromtxt(io.BytesIO(b))
array([[ 191.25 , 0. , 0. , 1. ],
[ 191.251, 0. , 0. , 1. ],
[ 191.252, 0. , 0. , 1. ]])
genfromtxt handles missing values, as well as offering much more control over how the final array is created.
Here's what I did to get the result you're looking for:
import numpy as np
b='191.250\t0.00\t0\t1\n191.251\t0.00\t0\t1\n191.252\t0.00\t0\t1\n'
a = np.array([[float(j) for j in i.split('\t')] for i in b.splitlines()])
Just turn the string into a list of strings (with split) and give that to array.
In [21]: np.array("1,Hey,234,4456,789".split(','))
Out[21]:
array(['1', 'Hey', '234', '4456', '789'],
dtype='|S4')
numpy.fromstring() is useful for reading numbers, but for tokenizing strings you can do this:
numpy.core.defchararray.split(data, sep=",")
Numpy as can handle it much easier than all the answers:
s = '[ 0. 0. 1.]'
np.fromstring(s[1:-1],sep=' ').astype(int)
IN:
import numpy as np
s = '[ 0. 0. 1.]'
out = np.array([int(i.replace('.','')) for i in s[s.find('[')+1:s.find(']')].split()])
print(type(out))
OUT:
<class 'numpy.ndarray'>
**Update (NumPy 1.19+):** Note that `.tostring()` is deprecated since NumPy 1.19. Use `.tobytes()` instead, which has identical behavior.
You can use the fromstring() method for this:
arr = np.array([1, 2, 3, 4, 5, 6])
ts = arr.tostring()
print(np.fromstring(ts, dtype=int))
>>> [1 2 3 4 5 6]
Sorry for the short answer, not enough points for commenting. Remember to state the data types or you'll end up in a world of pain.
Note on fromstring from numpy 1.14 onwards:
sep : str, optional
The string separating numbers in the data; extra whitespace between elements is also ignored.
Deprecated since version 1.14: Passing sep='', the default, is deprecated since it will trigger the deprecated binary mode of this function. This mode interprets string as binary bytes, rather than ASCII text with decimal numbers, an operation which is better spelt frombuffer(string, dtype, count). If string contains unicode text, the binary mode of fromstring will first encode it into bytes using either utf-8 (python 3) or the default encoding (python 2), neither of which produce sane results.
If you use tostring you lose information on both shape and data type:
>>> import numpy as np
>>> a = np.arange(12).reshape(3, 4)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> s = a.tostring()
>>> aa = np.fromstring(a)
>>> aa
array([ 0.00000000e+000, 4.94065646e-324, 9.88131292e-324,
1.48219694e-323, 1.97626258e-323, 2.47032823e-323,
2.96439388e-323, 3.45845952e-323, 3.95252517e-323,
4.44659081e-323, 4.94065646e-323, 5.43472210e-323])
>>> aa = np.fromstring(a, dtype=int)
>>> aa
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
>>> aa = np.fromstring(a, dtype=int).reshape(3, 4)
>>> aa
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
This means you have to send the metadata along with the data to the recipient. To exchange auto-consistent objects, try cPickle:
>>> import cPickle
>>> s = cPickle.dumps(a)
>>> cPickle.loads(s)
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])