使用\ n输出中的\ n替换文本

时间:2017-03-23 01:34:40

标签: python blank-line juniper

我正在尝试从juniper路由器获取配置,我遇到以下问题:

设置此

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

它给我带来了类似的东西

'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'

问题是我想以这种格式接收这些信息:

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

所以我希望\ n实际上是一个新行,而不仅仅是向我显示“\ n”字符串。

4 个答案:

答案 0 :(得分:7)

如果你在Python解释器中运行它,那么解释器的常规行为就是将换行符显示为" \ n"而不是实际的换行符,因为它使调试输出更容易。如果你想在解释器中获得实际的换行符,你应该print你得到的字符串。

如果这是程序输出的内容(即:您从外部程序获取新行转义序列),则应使用以下内容:

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

这将通过输出字符串中的实际换行符替换转义的换行符。

答案 1 :(得分:0)

您想要的替代方法是将您的字符串拆分为字符串列表(每行)。

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

答案 2 :(得分:0)

这也有效:

function checkCashRegister(price, cash, cid) {
  var change = [];
  let difference = cash - price;
  let level = 0;
  let currency = [
    ["PENNY", 0.01],
    ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["ONE", 1],
    ["FIVE", 5],
    ["TEN", 10],
    ["TWENTY", 20],
    ["ONE HUNDRED", 100]
  ]
  //sum all money in register
  let totalFunds = cid.reduce((r, [key, value]) => {
    return r + value
  }, 0)

  //function to match the difference to appropriate currency
  function findLevel(dif) {
    for (let i = currency.length - 1; i >= 0; i--) {
      //our change must be the highest availabe currency
      if (dif >= currency[i][1] && cid[i][1] != 0) {
        //console.log(currency[i])
        return i
      }
    }
  }
  //function to subtract
  function subtractor(dif, lvl) {
    if (cid[lvl][1] != 0) {
      dif = dif.toFixed(2)
      dif = dif - currency[lvl][1]
      cid[lvl][1] -= currency[lvl][1]
      change.push([...currency[lvl]])  //< -- PUSH A COPY!
      return dif
    }
  }
  //if no change needed
  if (difference == 0) {
    return {
      status: "CLOSED",
      change: []
    }
  }
  //if we dont have enough money to pay back
  if (totalFunds < difference) {
    return {
      status: "INSUFFICIENT_FUNDS",
      change: []
    }
  }
  //if we need to pay change AND we have enough money
  if (difference > 0 && totalFunds >= difference) {
    while (difference >= 0.01) {
      level = findLevel(difference)
      difference = subtractor(difference, level)
      //console.log(difference)
    }
  }

  //to sum duplicate elements in the "change" array, this is the problem!!!
  function sumDuplicates(arr1) {
    let arr = [...arr1]
    let sums = arr.reduce((sums, item) => {
      let found = sums.find(([key]) => key === item[0]);
      //console.log(found)
      if (found)
        found[1] += item[1];
      else
        sums.push(item);  
      // console.log(item)
      return sums;
    }, []);
    return sums;
  }
  let sums = sumDuplicates(change)
  console.log(sums);
  return {
    status: "OPEN",
    change: sums
  };
}

console.log(checkCashRegister(3.26, 100, [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100]
]));

答案 3 :(得分:0)

在python 3+中,解释所有转义字符的最佳方式是:

print(f"{yourStringHere}")

这使用了 f-strings,在我看来,这可能是解决这个问题的最优雅的方式。