Python numpy-tostring等价于numpy-fromstring

Python numpy-tostring等价于numpy-fromstring,python,numpy,Python,Numpy,Numpy有一个简洁的功能 它似乎也有一个整洁的功能 如何将\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00转换为一个普通的ASCII字符串,我可以将其写入文件?我在这里找到了我想要的我在这里找到了我想要的numpy数组有一个tostring()方法 In [1]: np.ones(3).tostring() Out[1]: b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x0

Numpy有一个简洁的功能

它似乎也有一个整洁的功能


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

我在这里找到了我想要的我在这里找到了我想要的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中却不同)。

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?'
请注意,名称是错误的,它返回的是字节而不是字符串(在Python2中正好相同,但在Python3中却不同)。

试试这个

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

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

将其写入文件有什么问题?你想写什么<代码>将open('x','w')作为f:f.write('\x00\x00\x00\x00\x01\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00)使用('\x00\x00\x00\x00\x00\x00\x00')
对我来说很好,将其写入文件有什么错?你想写什么<代码>以open('x','w')作为f:f.write('\x00\x00\x00\x00\x01\x00\x00\x00\x00\x02\x00\x00\x00\x00\x03\x00\x00\x00\x00')
适合我