[Numpy-discussion] String to integer array of ASCII values
Peter
numpy-discussion@maubp.freeserve.co...
Thu Jul 23 08:18:08 CDT 2009
Dear all,
I've looked over some of the documentation for creating an array, e.g.
http://docs.scipy.org/doc/numpy/user/basics.creation.html#arrays-creation
http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html
However, I don't see an example quite like what I want to do. I want
to be able to take a python string (e.g. "ABCDEF") and turn it into
an array of the ASCII values (i.e. [65, 66, 67, 68, 69, 70] for this
example).
>>> import numpy
>>> numpy.__version__
'1.1.1'
I can get the result I want like this, but I would like a faster way:
>>> numpy.array([ord(letter) for letter in "ABCDEF"])
array([65, 66, 67, 68, 69, 70])
I know in C that a string can been regarded as an array of unsigned
integers - so I'd like to get NumPy to do that for me. I'm guessing
there is a magic data type I can specify. Using "c" appears to mean
characters which is close but isn't what I want:
>>> numpy.array("ABCDEF", "c")
array(['A', 'B', 'C', 'D', 'E', 'F'],
dtype='|S1')
I eventually found this works:
>>> numpy.frombuffer("ABCDEF", numpy.byte)
array([65, 66, 67, 68, 69, 70], dtype=int8)
But why don't these work too?
>>> numpy.array("ABCDEF", numpy.byte)
Traceback (most recent call last):
...
ValueError: setting an array element with a sequence.
>>> numpy.fromiter("ABCDEF", numpy.byte, count=6)
Traceback (most recent call last):
...
ValueError: setting an array element with a sequence.
So, is using frombuffer the only or best option?
Thanks,
Peter
More information about the NumPy-Discussion
mailing list