Replacing a text with \n in it, with a real \n output

前端 未结 3 1476
情话喂你
情话喂你 2021-01-04 20:00

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(\         


        
相关标签:
3条回答
  • 2021-01-04 20:51

    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
    
    0 讨论(0)
  • 2021-01-04 20:59

    This also works:

    CONFIG = """{}""".format(CONFIG)
    
    0 讨论(0)
  • 2021-01-04 21:02

    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.

    0 讨论(0)
提交回复
热议问题