Q:

How can I remove \n and \r\n from stdout in python?

I have this script:
#!/usr/bin/python

import subprocess
import sys

HOST="cacamaca.caca"
COMMAND="display mac-address 0123-4567-8910"

ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND],
                       shell=False,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
if result == []:
    error = ssh.stderr.readlines()
    print >>sys.stderr, "ERROR: %s" % error
else:
    print result

Because the output has spaces and different lines, it prints carriage returns and new lines too so the result isn't a clean one:
['\r\n', 'cacamaca.caca\r\n', 'Info: The max number of VTY users is 10, and the number\r\n', '      of current VTY users on line is 2.\r\n', '      The current login time is 2017-07-20 20:10:54+03:00 DST.\r\n', '-------------------------------------------------------------------------------\r\n', 'MAC Address    VLAN/VSI                          Learned-From        Type      \r\n', '-------------------------------------------------------------------------------\r\n', '0123-4567-8910 1234/-                            Eth-Trunk9          dynamic   \r\n', '\r\n', '-------------------------------------------------------------------------------\r\n', 'Total items displayed = 1 \n', '\r\n', '']
How can I remove '\n' and '\r\n' or at least replace them with spaces so that the result looks like the original one? I did read a lot of answers about this, but none helped. 

A:

Your result variable is a list. I think you want to join the results into a single string and print that. You can do that with str.join() like this
print ''.join(result)

This will result in the following output 
cacamaca.caca
Info: The max number of VTY users is 10, and the number
 of current VTY users on line is 2.
 The current login time is 2017-07-20 20:10:54+03:00 DST.
-------------------------------------------------------------------------------
MAC Address VLAN/VSI Learned-From Type
-------------------------------------------------------------------------------
0123-4567-8910 1234/- Eth-Trunk9 dynamic

-------------------------------------------------------------------------------
Total items displayed = 1

