[Numpy-discussion] Getting number of *characters* in dtype='U' array

Robert Kern robert.kern@gmail....
Fri Sep 25 21:15:49 CDT 2009


On Fri, Sep 25, 2009 at 20:40, David Warde-Farley <dwf@cs.toronto.edu> wrote:
> On Fri, Sep 25, 2009 at 11:33:17AM -0400, Michael Droettboom wrote:
>> Is there a way to get the number of characters in a fixed-size 'U'
>> array?  I can, of course, parse dtype.str, or divide dtype.itemsize by
>> the size of a unicode character, but neither seems terribly elegant or
>> future proof.  Does numpy provide (to Python) a method for getting this
>> that I'm just missing?
>>
>> In [7]: x = np.array(u'1234')
>>
>> In [8]: x.dtype
>> Out[8]: dtype('<U4')
>>
>> In [9]: x.dtype.str
>> Out[9]: '<U4'
>>
>> In [10]: x.dtype.itemsize
>> Out[10]: 16
>
> I could be misleading you but I believe x.dtype.alignment is the divisor for itemsize that you're looking for?

Only by happenstance. It is not really related.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list