Bonjour à tous,
Je desire obtenir un format str a partir d'un format byte.
J'ai un programme qui fonctionne en Python 2 mais pas en Python 3.
En Python 2:
1 2 3 4 5 6 7 8
| 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", ""))
print (outList) |
RT
RT
ts
MI
VI
MO
VO
EM
EE
Mt
ce qui est exactement ce que je veux
lenI est un entier, files est un fichier en format binaire.
En Python 3:
1 2 3 4 5 6 7 8
| 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", ""))
print (outList) |
b'RT\x00\x00'
b'RT\x00\x00'
b'ts\x00\x00'
b'MI\x00\x00'
b'VI\x00\x00'
b'MO\x00\x00'
b'VO\x00\x00'
b'EM\x00\x00'
b'EE\x00\x00'
b'Mt\x00\x00'
j'ai essayé la chose suivante:
test = outList[0].decode('utf8')
AttributeError: 'str' object has no attribute 'decode'
Auriez-vous une idée?
Merci d'avance
Partager