[Numpy-discussion] astype char conversion

Travis Oliphant oliphant.travis at ieee.org
Wed Aug 9 17:11:49 CDT 2006


Matthew Brett wrote:
> Hi,
>
> Sorry if this is silly question, but should this work to convert from
> int8 to character type?
>
> a = array([104, 105], dtype=N.int8)
> a.astype('|S1')
>   
I'm not sure what you are trying to do here, but the standard coercion 
to strings will generate
['104', '105'].  However you are only allowing 1 character strings so 
you get the first character.

If you are wanting to get characters with ASCII codes 104 and 105 you 
can do that without coercion by simply viewing the memory as a different 
data-type:

a.view('S1')

array([h, i],
      dtype='|S1')


-Travis






More information about the Numpy-discussion mailing list