PiR*_*iRK 6 python string numpy python-3.x
我需要使用 Python 3 解码一个按以下方式编码的字符串:
>>> s = numpy.asarray(numpy.string_("hello\nworld"))
>>> s
array(b'hello\nworld',
dtype='|S11')
Run Code Online (Sandbox Code Playgroud)
我试过:
>>> 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
Run Code Online (Sandbox Code Playgroud)
如果我的理解是正确的,你可以这样做astype
, ifcopy = False
将返回包含相应类型内容的数组:
>>> s = numpy.asarray(numpy.string_("hello\nworld"))
>>> r = s.astype(str, copy=False)
>>> r
array('hello\nworld',
dtype='<U11')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8512 次 |
最近记录: |