I need to convert the byte string of a serialized object ( I used pickle ) to a normal string. The reason I need to do this is because I need to get the number of the package and I cannot convert b'0 to 0 directly.
I have tried to deserialize the byte string but it doesn't work. I even tried using .decode('utf-8') but as expected, it failed.
My UDP datagrams carry their index. It is the first thing attached to the pickled object, before #. I need to retrieve that number
How can I convert a serialized object byte string to regular string? Or if that isn't an option, what would be the best way to get my package number?
Because I cannot post an image, here is come context in code:
Server code:
while True:
print("In while.")
(received,address) = serverSocket.recvfrom(MAX_SIZE)
print("Received.")
#try:
print("In try.")
print("Raw received:",received)
print("Deserialized received:",pickle.load(received))
receivedPackageIndex = str(pickle.load(received)).split('#', 1)[0]
print("Received package:" , receivedPackageIndex)
if int(receivedPackageIndex) > maxPackageReceived:
print("Appending package ",receivedPackageIndex)
audioPicklestring += str(received).split("#",1)[1]
The output:
Received.
In try.
Raw received: b'0#\x80\x03cspeech_recognition\nAudioData\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00dataq\x03BM\xda\x00\x00fLaC\x00\x00\x00"\x10\x00\x10\x00\x00\x00\x00\x00\x00\x00\x03\xe8\x00\xf0\x00\x00\x98\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Traceback (most recent call last):
File "server.py", line 97, in <module>
main()
File "server.py", line 95, in main
getSoundFromClient()
File "server.py", line 61, in getSoundFromClient
print("Deserialized received:",pickle.load(received))
TypeError: file must have 'read' and 'readline' attributes