May-14-2019, 08:49 PM
Hello everyone!
I would like to extract the first bytes of a binary file, and convert them into strings, in a list.
files is the binary file.
This method works in Python 2 but not in Python 3
in Python 3, when I do
Thank you in advance for your help
I would like to extract the first bytes of a binary file, and convert them into strings, in a list.
def binaryMethod(files, lenI): outList = [] for _ in range(lenI): # read the length of the next string (read only the first 4 bytes) blen = int(ceil(float(unpack('>I', files.read(4))[0]) / 4) * 4) # store the string into outList outList.append(str(unpack('%ds' % blen, files.read(blen))[0]).replace("\x00", ""))where lenI is an integer.
files is the binary file.
This method works in Python 2 but not in Python 3

in Python 3, when I do
print (outList)
Output:["b\'RT\\\\x00\\\\x00\'", "b\'RT\\\\x00\\\\x00\'", "b\'ts\\\\x00\\\\x00\'", "b\'MI\\\\x00\\\\x00\', ..."
While I am expecting something like: ["RT1", "RT2, ""ts1", "MI1", ...] Thank you in advance for your help
