[Solved] Converting python float to bytes

I want to convert a Python float into a byte array, encoding it as a 32 bit little-endian IEEE floating point number, in order to write it to a binary file.

What is the modern Pythonic way to do that in Python 3? For ints I can do my_int.to_bytes(4,'little'), but there is no to_bytes method for floats.

It’s even better if I can do this in one shot for every float in a numpy array (with dtype numpy.float32). But note that I need to get it as a byte array, not just write the array to a file immediately.

There are some similar-sounding questions, but they seem mostly to be about getting the hex digits, not writing to a binary file.

Solution #1:

With the right dtype you can write the array’s data buffer to a bytestring or to a binary file:

In [449]: x = np.arange(4., dtype='<f4')                                        
In [450]: x                                                                     
Out[450]: array([0., 1., 2., 3.], dtype=float32)
In [451]: txt = x.tostring()                                                    
In [452]: txt                                                                   
Out[452]: b'[email protected]@@'
In [453]: x.tofile('test')                                                                                                                           
In [455]: np.fromfile('test','<f4')                                             
Out[455]: array([0., 1., 2., 3.], dtype=float32)
In [459]: with open('test','br') as f: print(f.read())                          
b'[email protected]@@'

Change endedness:

In [460]: x.astype('>f4').tostring()                                            
Out[460]: b'[email protected]@@x00x00'
Respondent: Nathaniel

Solution #2:

You could use struct to pack the bytes like,

>>> import struct
>>> struct.pack('<f', 3.14) # little-endian
b'[email protected]'
>>> struct.pack('>f', 3.14) # big-endian
b'@Hxf5xc3'
Respondent: hpaulj

Solution #3:

NumPy arrays come with a tobytes method that gives you a dump of their raw data bytes:

arr.tobytes()

You can specify an order argument to use either C-order (row major) or F-order (column major) for multidimensional arrays.

Since you want to dump the bytes to a file, you may also be interested in the tofile method, which dumps the bytes to a file directly:

arr.tofile(your_file)

tofile always uses C-order.

If you need to change endianness, you can use the byteswap method. (newbyteorder has a more convenient signature, but doesn’t change the underlying bytes, so it won’t affect tobytes.)

import sys
if sys.byteorder=='big':
    arr = arr.byteswap()
data_bytes = arr.tobytes()
Respondent: han solo

Solution #4:

There are save/savez methods in numpy:

Store data to disk, and load it again:

>>> np.save('/tmp/123', np.array([[1, 2, 3], [4, 5, 6]]))
>>> np.load('/tmp/123.npy')
array([[1, 2, 3],
       [4, 5, 6]])

Store compressed data to disk, and load it again:

>>> a=np.array([[1, 2, 3], [4, 5, 6]])
>>> b=np.array([1, 2])
>>> np.savez('/tmp/123.npz', a=a, b=b)
>>> data = np.load('/tmp/123.npz')
>>> data['a']
array([[1, 2, 3],
       [4, 5, 6]])
>>> data['b']
array([1, 2])
>>> data.close()

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.