Replacing a text with \n in it, with a real \n output [duplicate]
Asked Answered
E

4

31

I am trying to get a config from a juniper router and I have the following problem:

After setting this

stdin, stdout, stderr = client1.exec_command('show configuration interfaces %s' % SID)
 CONFIG = stdout.read()
 print CONFIG

It brings me something like these

'description El_otro_Puerto_de_la_Routing-instance;\nvlan-id 309;\nfamily inet {\n    mtu 1600;\n    address 10.100.10.10/24;\n}\n'

and the problem is that I want to receive that information in this format:

'description El_otro_Puerto_de_la_Routing-instance;
 nvlan-id 309;
 nfamily inet {
  mtu 1600;
  address 10.100.10.10/24;
 }

So I want the \n to actually be a new line, and not just to show me the "\n" string.

Engrossing answered 23/3, 2017 at 1:34 Comment(4)
What does print repr(CONFIG) show?Sulphathiazole
Does the string print with quotation marks in it? Looks like it's a string representation of a string.Constipate
See #24886623Vanden
Related: #48908631Roadhouse
G
46

If you're running this in the Python interpreter, it is the regular behavior of the interpreter to show newlines as "\n" instead of actual newlines, because it makes it easier to debug the output. If you want to get actual newlines within the interpreter, you should print the string you get.

If this is what the program is outputting (i.e.: You're getting newline escape sequences from the external program), you should use the following:

OUTPUT = stdout.read()
formatted_output = OUTPUT.replace('\\n', '\n').replace('\\t', '\t')
print formatted_output

This will replace escaped newlines by actual newlines in the output string.

Gerber answered 23/3, 2017 at 1:42 Comment(2)
I'd like to use eval() for supporting parsing any possible escape sequence. for example: formatted_output=eval("'{}'".format(OUTPUT)Photic
Never run eval() on untrusted input.Gerber
O
8

In python 3+, the best way to interpret all escape characters is:

print(f"{yourStringHere}")

This uses f-strings which, in my opinion, is probably the most elegant way to solve this issue.

Oppilate answered 18/5, 2021 at 16:26 Comment(1)
New to f-strings... but this doesn't work for me (3.7). Have I missed something? >>> mystring = r"this\nthat\tthe other" >>> print(f"{mystring}") this\nthat\tthe otherChloropicrin
L
0

An alternative to what you want is splitting your string into a list of strings (per line).

mystr = 'a\nb\nc'
mystr = mystr.split(sep='\n')
print(mystr)
#this will be your print output:['a', 'b', 'c']
for s in mystr:
    print(s)
#your print output:
#a
#b
#c
Liquidate answered 23/3, 2017 at 1:42 Comment(3)
I believe this was not the author's question. I believe what they were trying to do was to replace the escape sequence '\n' by an actual newline character.Gerber
Read where it says "an alternative to what you want". The person has not said if this question refers specifically to the interpreter, a console (cmd in windows), or writing to file.Liquidate
Agree that this is useful. As an efficiency improvement we could replace the loop with print("\n".join(map(str, mystr)))Potted
B
0

A regular expression can help with locating the \x sequences. The substitution-pairs will still have to be specified manually though, just like in @PedroCastilho's answer.

import re
CONFIG = stdout.read()
print(re.sub(r'\\.',lambda x:{'\\n':'\n','\\t':'\t'}.get(x[0],x[0]),CONFIG))

This will replace \\n-s and \\t-s with actual line breaks and tabulators, and leaves the unknown ones intact.

Like

print(re.sub(r'\\.',lambda x:{'\\n':'\n','\\t':'\t'}.get(x[0],x[0]),
      'foo\\nbar\\nbaz\\tbrr\\n\\q'))

prints

foo
bar
baz     brr
\q

So line breaks and the tabulator are in place, and \q simply survived.

Basuto answered 11/4, 2022 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.