我需要解码,使用Python 3,经编码方式如下字符串:如何解码dtype = numpy.string_的numpy数组?
>>> s = numpy.asarray(numpy.string_("hello\nworld"))
>>> s
array(b'hello\nworld',
dtype='|S11')
我想:
>>> str(s)
"b'hello\\nworld'"
>>> s.decode()
AttributeError Traceback (most recent call last)
<ipython-input-31-7f8dd6e0676b> in <module>()
----> 1 s.decode()
AttributeError: 'numpy.ndarray' object has no attribute 'decode'
>>> s[0].decode()
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-34-fae1dad6938f> in <module>()
----> 1 s[0].decode()
IndexError: 0-d arrays can't be indexed
谢谢!这有很大帮助。现在我可以通过这种方式恢复我的字符串:'s = str(s.astype(str))' – PiRK
当你可以直接用'unicode_'获得常规字符串时,不需要转换类型。 – Kasramvd
我不控制编码阶段。在我现实世界的问题中,我自己并没有创造's'。我只是碰巧知道它在编码阶段后写入了一个文件。 – PiRK