String to integer array of ASCII values
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
On Thu, Jul 23, 2009 at 7:18 AM, Peter < numpy-discussion@maubp.freeserve.co.uk> wrote:
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?
Would something like In [2]: array("ABCDEF", 'c').view(uint8) Out[2]: array([65, 66, 67, 68, 69, 70], dtype=uint8) work for you? Chuck
On Thu, Jul 23, 2009 at 3:54 PM, Charles R
Harris
Would something like
In [2]: array("ABCDEF", 'c').view(uint8) Out[2]: array([65, 66, 67, 68, 69, 70], dtype=uint8)
work for you?
Yes, that also looks good. I guess I have a couple of options to benchmark now :) Thank you, Peter
On Thu, Jul 23, 2009 at 9:50 AM, Peter < numpy-discussion@maubp.freeserve.co.uk> wrote:
On Thu, Jul 23, 2009 at 3:54 PM, Charles R Harris
wrote: Would something like
In [2]: array("ABCDEF", 'c').view(uint8) Out[2]: array([65, 66, 67, 68, 69, 70], dtype=uint8)
work for you?
Yes, that also looks good. I guess I have a couple of options to benchmark now :)
One more: In [1]: fromstring("ABCDEF", dtype=uint8) Out[1]: array([65, 66, 67, 68, 69, 70], dtype=uint8) Chuck
On Thu, Jul 23, 2009 at 5:07 PM, Charles R
Harris
One more:
In [1]: fromstring("ABCDEF", dtype=uint8) Out[1]: array([65, 66, 67, 68, 69, 70], dtype=uint8)
Chuck
I should have guessed that one. Why isn't numpy.fromstring listed with the other entries in the "From existing data" section here? http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html This looks like a simple improvement to the documentation... Peter
participants (2)
-
Charles R Harris
-
Peter