0

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
hunch_hunch
  • 2,283
  • 1
  • 21
  • 26
Michael
  • 1
  • 2

1 Answers1

0

Don't know what are you trying to achieve.Pickle is mainly used for serializing and de-serializing a Python object structure. We can use pickle.load() to load an object from an object file(.obj). So in here

print("Deserialized received:",pickle.load(received))

is not at all a file. The method recvfrom() is used to recieve an UDP message. If you Want to know how to serialize and de-serialize an object please look at here

Community
  • 1
  • 1
Chiyaan Suraj
  • 1,021
  • 2
  • 13
  • 27
  • I am trying to do exactly that, serialize and de-serialize a Python object structure. However, because my datagram is rather big, after pickling it on the client, I send several smaller datagrams to the server. However, I need to put them back correctly, so before every datagram, I add it's index. Bassicly, my server will seceive string like "0#\x80\x03cspeech_recognition\nAudioData\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00dataq\x03BM\xda\ ...". I need to look at the number before "#" in order to know if I am to append what is after it, to reconstruct the original pickestring or not. – Michael Apr 14 '15 at 09:56
  • When I try to convert int(b'0') I get an error, because a byte string cannot be converted to int like a normal string can. I want to take the b'0#' out of my received byte string and turn it into a normal string so I can get the 0 out. – Michael Apr 14 '15 at 09:59
  • Ok last chance. the way you can convert `b'0'` to 0 is `int(str(b'0'))`. coming to desrializing.To desrialize a byte array you can do 1) If you are sure that whatever the string you are getting from your server is desrialized and if you want to use `pickle.loads()` then you can just write it to an object file and load it back using loads function. 2) There is a module called 'cPickle' which can help you to desrialize in an easy way. you just have to say `cPickle.loads(yourstring)` to desrialze a serilaized object. hope it helps you a bit. – Chiyaan Suraj Apr 14 '15 at 11:05