英文:
output in python not as same as python3 (same script)
问题
在Python 2中的输出:
Rp#sh int tra
If device is externally calibrated, only calibrated values are printed.
++ : high alarm, + : high warning, - : low warning, -- : low alarm.
NA or N/A: not applicable, Tx: transmit, Rx: receive.
mA: milliamperes, dBm: decibels (milliwatts).
Optical Optical
Temperature Voltage Tx Power Rx Power
Port (Celsius) (Volts) (dBm) (dBm)
--------- ----------- ------- -------- --------
Te1/1/1 24.3 3.26 -2.9 -1.8
Te1/1/3 34.7 3.35 -1.1 -13.7
Te1/1/4 32.6 3.20 -3.5 -1.6
在Python 3中的输出:
b'\r\nRp#sh int tra\r\nIf device is externally calibrated, only calibrated values are
printed.\r\n++ : high alarm, + : high warning, - : low warning, -- : low alarm.\r\nNA or
N/A: not applicable, Tx: transmit, Rx: receive.\r\nmA: milliamperes, dBm: decibels
(milliwatts).\r\n\r\n Optical Optical\r\n Temperature Voltage Tx Power Rx Power\r\nPort (Celsius) (Volts) (dBm) (dBm)\r
\n--------- ----------- ------- -------- --------\r\nTe1/1/1 25.2 3.26 -2.9 -1.8 \r\nTe1/1/3 35.5 3.35 -1.1 -13.6 \r\nTe1/1/4 33.6 3.21 -3.5 -1.6 \r\n\r\n\r\n'
英文:
i write my code in python 2 and everything going well but when i use the same code in python3 the output not so fit as i want or not look as in python 2 how i solve this issue ?
import paramiko
import time
UserName = "*****"
Password = "******"
ip = "10.226.159.1"
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip, username=UserName, password=Password)
remote_connection = ssh_client.invoke_shell()
remote_connection.send("show interfaces transceiver \n")
time.sleep(10)
output = remote_connection.recv(65535)
print (output)
the output in python2 :
Rp#sh int tra
If device is externally calibrated, only calibrated values are printed.
++ : high alarm, + : high warning, - : low warning, -- : low alarm.
NA or N/A: not applicable, Tx: transmit, Rx: receive.
mA: milliamperes, dBm: decibels (milliwatts).
Optical Optical
Temperature Voltage Tx Power Rx Power
Port (Celsius) (Volts) (dBm) (dBm)
--------- ----------- ------- -------- --------
Te1/1/1 24.3 3.26 -2.9 -1.8
Te1/1/3 34.7 3.35 -1.1 -13.7
Te1/1/4 32.6 3.20 -3.5 -1.6
the output in python 3:
b'\r\nRp#sh int tra\r\nIf device is externally calibrated, only calibrated values are
printed.\r\n++ : high alarm, + : high warning, - : low warning, -- : low alarm.\r\nNA or
N/A: not applicable, Tx: transmit, Rx: receive.\r\nmA: milliamperes, dBm: decibels
(milliwatts).\r\n\r\n Optical Optical\r\n Temperature Voltage Tx Power Rx Power\r\nPort (Celsius) (Volts) (dBm) (dBm)\r
\n--------- ----------- ------- -------- --------\r\nTe1/1/1 25.2 3.26 -2.9 -1.8 \r\nTe1/1/3 35.5 3.35 -1.1 -13.6 \r\nTe1/1/4 33.6 3.21 -3.5 -1.6 \r\n\r\n\r\n
答案1
得分: 2
To print the output correctly in python 3, you need to change the datatype from bytes to string:
>>> print(output.decode())
Rp#sh int tra
If device is externally calibrated, only calibrated values are printed.
++ : high alarm, + : high warning, - : low warning, -- : low alarm.
NA or N/A: not applicable, Tx: transmit, Rx: receive.
mA: milliamperes, dBm: decibels (milliwatts).
Optical Optical
Temperature Voltage Tx Power Rx Power
Port (Celsius) (Volts) (dBm) (dBm)
--------- ----------- ------- -------- --------
Te1/1/1 25.2 3.26 -2.9 -1.8
Te1/1/3 35.5 3.35 -1.1 -13.6
Te1/1/4 33.6 3.21 -3.5 -1.6
英文:
To print the output correctly in python 3, you need to change the datatype from bytes to string:
>>> print(output.decode())
Rp#sh int tra
If device is externally calibrated, only calibrated values are printed.
++ : high alarm, + : high warning, - : low warning, -- : low alarm.
NA or N/A: not applicable, Tx: transmit, Rx: receive.
mA: milliamperes, dBm: decibels (milliwatts).
Optical Optical
Temperature Voltage Tx Power Rx Power
Port (Celsius) (Volts) (dBm) (dBm)
--------- ----------- ------- -------- --------
Te1/1/1 25.2 3.26 -2.9 -1.8
Te1/1/3 35.5 3.35 -1.1 -13.6
Te1/1/4 33.6 3.21 -3.5 -1.6
答案2
得分: 2
以b
开头的字符串是字节文字(在此处查看更多详细信息https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal)
要打印字节文字,您需要先将其转换为字符串:
# 检查是否为字符串或字节。如有必要,转换为字符串
if type(output) == bytes:
output = output.decode()
print(output)
这段代码应该在Python 2和Python 3中都有效。
英文:
String prefixed with b
is a bytes literal (see more details here https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal)
To print bytes literal you need to convert it into a string first:
# Check if we have string or bytes. Convert to string, if necessary
if type(output) == bytes:
output = output.decode()
print(output)
This code should work both in Python 2 and Python 3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论