0

I receive data from a socket in Python 3 with

data = ""
...
data = s.recv(1024)

When I want to print(data) and print (type(data)), it tells me:

b'contentofdata'
<class 'bytes'>

How can I convert data back to a string so it only shows contentofdata after print(data)? I tried a lot with .decode or .encode, but it does not seem to work in Python 3.

1
  • 1
    It's just data.decode(). Commented Aug 12, 2016 at 2:02

1 Answer 1

1

You can just do str(data, 'utf-8') to convert bytes to string.

Sign up to request clarification or add additional context in comments.

Comments

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.