1

After sending some information through a socket i have a binary object that looks like this:

b"1:b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'"

What I want to do is get the first part before the : as a string and the second as a binary. Like this:

'1'

and:

b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'

With all my attempts I ended up with either:

b"b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'"

or:

"b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'"

1 Answer 1

1

Just split it on b':' and decode and trim accordingly:

i, j = r.split(b':')
i = i.decode()  #  '1'
j = j[2:-1]

Now:

print(i)
# 1
print(j)
# b'5Q\x19aw\x17\x8c\x98\x10\x1c\xe0O\x14\xd1x\xa1'
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, so simple. Actually instead of j = j[2:-1] i just used j. Otherwise i get only part of the message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.