Ingrid
Ingrid

Reputation: 76

Convert strings of bytes to floats in Python

I'm using Pandas to handle my data. The first step of my script is to convert a column of strings of bytes to a list of floats. My script is working but its taking too long. Any suggestions on how to speed it up??

def byte_to_hex(byte_str):
    array = ["%02X" % ord(chr(x)) for x in byte_str]
    return array

for index, row in enumerate(data[column].values):
    row_new = []
    row = byte_to_hex(row)
    stop = len(row)
    for i in range(4, stop, 4):
        sample = "".join(row[i:i + 4])
        row_new.append(struct.unpack('!f', bytes.fromhex(sample))[0])

Example:
b'\x00\x00\x00\x1cB\x80\x1f\xfcB\x80\x1f\xfcB\x80w\xc8Bz\xa1\x97B\x80\x1f\xfcB}LZB\x80\x1f\xfcBz\xa1\x97Bz\xcaoB\x803\xf5B}\xc5\x84B\x80w\xc8B}\xed\xdbB\x80\x1f\xfcB}\xc5\x84B}LZB\x80\x1f\xfcB}#\xe9B}\xed\xdbB}\xc5\x84B\x803\xf5B\x80\x1f\xfcB}\xc5\x84B\x803\xf5B\x803\xf5Bx\xef\tB\x81\xc4\xdf\x7f\xff\xff\xff'

[64.06246948242188, 64.06246948242188, 64.23394775390625, 62.65780258178711, 64.06246948242188, 63.324562072753906, 64.06246948242188, 62.65780258178711, 62.697689056396484, 64.10147857666016, 63.44288635253906, 64.23394775390625, 63.48228073120117, 64.06246948242188, 63.44288635253906, 63.324562072753906, 64.06246948242188, 63.28506851196289, 63.48228073120117, 63.44288635253906, 64.10147857666016, 64.06246948242188, 63.44288635253906, 64.10147857666016, 64.10147857666016]

Very thankful for any help :)

Upvotes: 1

Views: 2407

Answers (1)

SerialDev
SerialDev

Reputation: 2847

I think you are looking for the Struct package

import struct

struct.pack('f', 3.14)
OUT: b'\xdb\x0'

struct.unpack('f', b'\xdb\x0fI@')
OUT: (3.1415927410125732,)

struct.pack('4f', 1.0, 2.0, 3.0, 4.0)
OUT: '\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@'

Upvotes: 2

Related Questions