为什么我在每行的开头得到 b' 而在每行的末尾得到 '?

Why do I get b' at the start of each line and ' at the end of each line?

我有一个使用 pxssh 的 python 脚本。这是脚本:

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)

我不明白为什么脚本输出是这样的:

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

每行开头的 b' 和每行输出末尾的 ' 是怎么回事?如果它我该如何摆脱?

来自文档:

Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.

https://docs.python.org/3.3/reference/lexical_analysis.html#string-literals

是二进制数据,需要用str()转换。

print( str( line, 'utf-8' ) )

还有许多其他字符串格式,例如 iso-8859-1iso-8859-16 等。 但是,如果有疑问,请先尝试 utf-8