用真实的\ n输出替换其中带有\ n的文本 [英] Replacing a text with \n in it, with a real \n output
问题描述
我正在尝试从杜松路由器中获取配置,但是我遇到以下问题:
I am trying to get a config from a juniper router and I have the following problem:
设置后
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;
}
所以我希望\ n实际上是换行符,而不仅仅是给我显示"\ n"字符串.
So I want the \n to actually be a new line, and not just to show me the "\n" string.
推荐答案
如果您是在Python解释器中运行此程序,则解释器的常规行为是将换行符显示为"\ n".而不是实际的换行符,因为这样可以更轻松地调试输出.如果要在解释器中获取实际的换行符,则应print
获得的字符串.
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.
这篇关于用真实的\ n输出替换其中带有\ n的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!