2013-04-08 120 views
2

numpy有一个整洁的功能numpy.fromstringnumpy tostring相当于numpy fromstring

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

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

+0

写什么错写入文件?你想写什么呢? ('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

回答

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

注意,名字是错误的,它返回字节而不是字符串(这恰好是在同蟒蛇2但不是蟒蛇3)。

0

试试这个。

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