[Numpy-discussion] Unicode characters in a numpy array

Gökhan Sever gokhansever@gmail....
Wed Jun 15 14:30:56 CDT 2011


Hello,

The following snippet works fine for a regular string and prints out
the string without a problem.

python
Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> mystr = u"öööğğğ"
>>> mystr
u'\xf6\xf6\xf6\u011f\u011f\u011f'
>>> type(mystr)
<type 'unicode'>
>>> print mystr
öööğğğ

What is the correct way to print out the following array?

>>> import numpy as np
>>> arr = np.array(u"öööğğğ")
>>> arr
array(u'\xf6\xf6\xf6\u011f\u011f\u011f',
      dtype='<U6')
>>> print arr
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/site-packages/numpy/core/numeric.py",
line 1379, in array_str
    return array2string(a, max_line_width, precision, suppress_small,
' ', "", str)
  File "/usr/lib64/python2.7/site-packages/numpy/core/arrayprint.py",
line 426, in array2string
    lst = style(x)
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-5: ordinal not in range(128)


Thanks.

-- 
Gökhan


More information about the NumPy-Discussion mailing list