[英]Why do I get b' at the start of each line and ' at the end of each line?
I have this python script that uses pxssh. 我有这个使用pxssh的python脚本。 Here's the script:
这是脚本:
from pexpect import pxssh
import getpass
try:
s = pxssh.pxssh()
hostname = input('hostname: ')
username = input('username: ')
password = getpass.getpass('password: ')
s.login(hostname, username, password)
s.sendline('cat hiera/my.yaml') # run a command
s.prompt() # match the prompt
for line in s.before.splitlines()[1:]:
print (line)
s.logout()
except pxssh.ExceptionPxssh as e:
print("pxssh failed on login.")
print(e)
and I cannot understand why the script output looks like this: 我不明白为什么脚本输出如下所示:
b'---'
b'settings_prod: |'
b'"""'
b"Generated by 'django-admin startproject' using Django 2.0.5."
b''
b'For more information on this file, see'
b'https://docs.djangoproject.com/en/2.0/topics/settings/'
b''
b'For the full list of settings and their values, see'
b'https://docs.djangoproject.com/en/2.0/ref/settings/'
b'"""'
What is up with the b'
at the start of each line and the '
on the end of each line of output? 输出的每一行开头的
b'
和输出的每一行末尾的'
是什么? How do I get rid if it? 我要如何摆脱呢?
From the docs: 从文档:
Bytes literals are always prefixed with 'b' or 'B';
字节字面量始终以“ b”或“ B”为前缀; they produce an instance of the bytes type instead of the str type.
它们产生字节类型而不是str类型的实例。 They may only contain ASCII characters;
它们只能包含ASCII字符; bytes with a numeric value of 128 or greater must be expressed with escapes.
数值等于或大于128的字节必须用转义符表示。
https://docs.python.org/3.3/reference/lexical_analysis.html#string-literals https://docs.python.org/3.3/reference/lexical_analysis.html#string-literals
It's binary data, it needs to be converted with str()
. 它是二进制数据,需要使用
str()
进行转换。
print( str( line, 'utf-8' ) )
There's a bunch of other string formats, like iso-8859-1
, iso-8859-16
, etc. But, if in doubt, try utf-8
first. 还有许多其他字符串格式,例如
iso-8859-1
, iso-8859-16
等。但是,如有疑问,请首先尝试utf-8
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.