[SciPy-user] Print unicode objects in object arrays

Robert Kern robert.kern@gmail....
Tue May 26 17:43:22 CDT 2009


On Tue, May 26, 2009 at 17:33,  <mmanns@gmx.net> wrote:
> Hi
>
> Is there a way of printing unicode objects that are inside an object
> array?
>
> $ python
> Python 2.5.4 (r254:67916, Feb 17 2009, 20:16:45)
> [GCC 4.3.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> u = [u'\u201e']
>>>> u
> [u'\u201e']
>>>> import numpy
>>>> a = numpy.array(u, dtype="O")
>>>> a
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
>  File "/usr/lib/python2.5/site-packages/numpy/core/numeric.py", line
> 1088, in array_repr ', ', "array(")
>  File "/usr/lib/python2.5/site-packages/numpy/core/arrayprint.py",
> line 287, in array2string separator, prefix)
>  File "/usr/lib/python2.5/site-packages/numpy/core/arrayprint.py",
> line 216, in _array2string _summaryEdgeItems, summary_insert)[:-1]
>  File "/usr/lib/python2.5/site-packages/numpy/core/arrayprint.py",
> line 333, in _formatArray word = format_function(a[-1])
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u201e' in
> position 0: ordinal not in range(128)
>>>> numpy.__version__
> '1.2.1'

Hmm, looks like we use str() for object arrays instead of repr(). That
is unfortunate. You can work around this with a hack:

In [18]: class array(np.ndarray):
   ....:     _format = repr
   ....:
   ....:

In [20]: a = empty(1, object)

In [21]: a[0] = u'\u201e'

In [22]: a.view(array)
Out[22]: array([u'\u201e'], dtype=object)


-- 
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 SciPy-user mailing list