How To Get Python To Make New Lines In A Print Statement With \r\n?
Right now, I'm coding a python SMTP script. status = clientSocket.recv(1024) print (status) When python prints status, I get: b'250-mx.google.com at your service, [107.216.175.252
Solution 1:
status
has bytes type, so you need to decode it to string.
2 obvious ways to achieve this:
print(status.decode())
print(str(status, 'utf-8'))
The default encoding for .decode
is UTF-8, so if you want to use a different one, do status.decode(encoding)
.
And status.decode(encoding)
is exact equivalent to str(status, encoding)
.
Why just str(status)
isn't working:
From the documentation on str
function:
Passing a bytes object to str() without the encoding or errors arguments falls under the first case of returning the informal string representation. For example:
>>> str(b'Zoot!')
"b'Zoot!'"
Solution 2:
>>>print(status.decode())
250-mx.google.com at your service, [107.216.175.252]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
250-ENHANCEDSTATUSCODES
250 CHUNKING
You're on Python 3 which defaults to unicode, but you have an old style byte-string from doing sockets. You just have to decode
it and then the print
will get rid of newline representations as usual.
Post a Comment for "How To Get Python To Make New Lines In A Print Statement With \r\n?"