How to decode a numpy array of dtype=numpy.string_?
<p>I need to decode, with Python 3, a string that was encoded the following way:</p>

<pre><code>&gt;&gt;&gt; s = numpy.asarray(numpy.string_("hello\nworld"))
&gt;&gt;&gt; s
array(b'hello\nworld', 
      dtype='|S11')
</code></pre>

<p>I tried:</p>

<pre><code>&gt;&gt;&gt; str(s)
"b'hello\\nworld'"

&gt;&gt;&gt; s.decode()
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-31-7f8dd6e0676b&gt; in &lt;module&gt;()
----&gt; 1 s.decode()

AttributeError: 'numpy.ndarray' object has no attribute 'decode'

&gt;&gt;&gt; s[0].decode()
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-34-fae1dad6938f&gt; in &lt;module&gt;()
----&gt; 1 s[0].decode()

IndexError: 0-d arrays can't be indexed
</code></pre>

<p>If my understanding is correct, you can do this with <code>astype</code> which, if <code>copy = False</code> will return the array with the contents in the corresponding type:</p>

<pre><code>&gt;&gt;&gt; s = numpy.asarray(numpy.string_("hello\nworld"))
&gt;&gt;&gt; r = s.astype(str, copy=False)
&gt;&gt;&gt; r 
array('hello\nworld', 
      dtype='&lt;U11')
</code></pre>

python string python-3.x numpy