[Numpy-discussion] String to integer array of ASCII values
Charles R Harris
charlesr.harris@gmail....
Thu Jul 23 09:54:16 CDT 2009
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20090723/441c93aa/attachment.html
More information about the NumPy-Discussion
mailing list