-2

I am writing an application in Python, I am using pexpect ( more specifically the pxssh functionality) to execute a series of commands on a Cisco Router. I want to store the output returned to a variable intact with the whitespaces and the newlines.

s = pxssh.pxssh(options={
        "StrictHostKeyChecking": "no",
        "UserKnownHostsFile": "/dev/null"})

After login and execute relevant command, I store the result in a dictionary:

data[command]=repr(s.before)

repr returns /n and the /r perfectly, however whitespaces are missing.

As an example the print(s.before) returns as below.

Snapshot of the print(s.before)

However with repr the best I am able to get (and consequently format) is as below:

snapshot when using repr

In where whitespaces and formatting are lost.

P.S. : I cannot write and read to the files. Any help appreciated.

thewaywewere
  • 8,128
  • 11
  • 41
  • 46
  • What do you use to get this output? print? repr? – Slam May 29 '17 at 11:46
  • The first image is simply a print(s.before) - which is the format I want and the second image is when I store the s.before in a variable like : mytext= repr(s.before) and then replace the /n and /r with
    { These are being printed in a html div}
    – Sakshi Bhutani May 29 '17 at 11:53
  • The newline characters are preserved with repr but not the whitespaces, I want them spaces! – Sakshi Bhutani May 29 '17 at 11:54
  • Please don't post screenshots of text. Copy/paste (or transcribe) the text directly into the question. – BSMP May 29 '17 at 16:27

1 Answers1

0

As far as I understand, you're trying to save python string (with \n and whitespaces) and inject it in some html.

If you're having trouble with html view, wrap your string into <pre> tag to preserve formatting (including spaces). This is not issue with python (again, as far as I understand), you can check this by printing your variable or counting whitespaces inside python

Slam
  • 8,112
  • 1
  • 36
  • 44
  • Nope. I am unable to get the right spacing from the terminal output itself. I have tried repr and even redirecting the standard stdout. I get the new lines but not the spaces. – Sakshi Bhutani May 29 '17 at 16:31
  • A combination of redirecting the standard stdout and
     worked. Thank you for the tip.
    – Sakshi Bhutani May 29 '17 at 17:20