代码之家  ›  专栏  ›  技术社区  ›  Red Cricket

为什么我在每行的开头和结尾都有“b”?

  •  0
  • Red Cricket  · 技术社区  · 7 年前

    我有一个使用pxsh的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' 在每行和 '

    2 回复  |  直到 7 年前
        1
  •  0
  •   Kingsley    7 年前

    它是二进制数据,需要用 str() .

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

    还有很多其他的字符串格式,比如 iso-8859-1 , iso-8859-16 utf-8 第一。

        2
  •  1
  •   user5597655 user5597655    7 年前

    从文档中:

    字节类型而不是str类型的实例。他们可能只是 必须用转义来表达。

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

    推荐文章