numpy tostring相当于numpy fromstring

时间:2013-04-08 16:37:54

标签: python numpy

Numpy有一个整洁的功能numpy.fromstring

它似乎也有一个整洁的功能numpy.chararray.tostring

如何将\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00转换为普通的ASCII字符串,我可以将其写入文件?

3 个答案:

答案 0 :(得分:2)

我找到了我在这里寻找的东西numpy.ndarray.tofile

答案 1 :(得分:2)

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?'

请注意,该名称是错误的,它返回字节而不是字符串(在python 2中恰好相同但不是python 3)。

答案 2 :(得分:0)

试试这个。

np.fromstring('\x01\x02', dtype=np.uint8)