numpy有一个整洁的功能numpy.fromstring
。numpy tostring相当于numpy fromstring
它也似乎有一个整洁的功能numpy.chararray.tostring
。
如何将\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00
转换为正常的ASCII字符串,我可以写入文件?
numpy有一个整洁的功能numpy.fromstring
。numpy tostring相当于numpy fromstring
它也似乎有一个整洁的功能numpy.chararray.tostring
。
如何将\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00
转换为正常的ASCII字符串,我可以写入文件?
我发现我一直在寻找在这里numpy.ndarray.tofile
numpy的阵列具有tostring()
方法
In [1]: np.ones(3).tostring()
Out[1]: b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\xf0?'
注意,名字是错误的,它返回字节而不是字符串(这恰好是在同蟒蛇2但不是蟒蛇3)。
试试这个。
np.fromstring('\x01\x02', dtype=np.uint8)
写什么错写入文件?你想写什么呢? ('x','w')作为f:f.write('\ x00 \ x00 \ x00 \ x00 \ x00 \ x00 \ x02 \ x00 \ x00 \ x00 \ x00 \ x03 \ x00 \ x00 \ x00')'适合我 – 2013-04-08 16:45:24