[Numpy-discussion] converting "numpy.bytes_" from numpy 1.7.0 to "numpy.string_" of numpy 1.6.1

Chris Barker - NOAA Federal chris.barker@noaa....
Sun Apr 7 15:57:22 CDT 2013


On Sun, Apr 7, 2013 at 11:28 AM, Sergio Rojas <sergio_r@mail.com> wrote:
> I am using a function which under python 2.2.7 and numpy 1.6.1
> returns a "list" (called d) whose elements are of type
> "numpy.string_" (see below).
>
> Under python 3.3.0 and numpy 1.7.0 the same function returns the
> list as an object of type "builtins.list" whose elements are
> of type "numpy.bytes_".

1st, jsut to be clear, a "list" in py2 is a "builtins.list" in py3.

But the more interesting part: A "string" in py2 is a "bytes_" object in py3.

In Py2 a string was originaly a 1-byte per character ansi string - i,e
a char* under the hood, and identical to a a string of bytes.  In py3,
a "sring" is a unicode string (same as a unicode object on py2). To
occomidate this, the bytes object was intriduced (lso in py2, but it
py 2 a bytes and a string ar the same thing...). A numpy "string" type
is more like the py2 string -- i.e. a string of single bytes. This is
whyou get a bytes objects from it. Numpy has no unicode support built
in. So what you've got is probably what you want.

If you do want py3 strings (i.e. unicode), you'll need to decode the
numpy bytes object to make a string. Sorry, I'm not running py3, so
not sure the exact syntax, but in py2, it's somethign like:

list_of_unicode_strings = [b.decode('ascii') for b in list_of_bytes_objects]


HTH,
  -Chris


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the NumPy-Discussion mailing list