AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-406443

Kevin Rutan's questions

Martin Hope
Kevin Rutan
Asked: 2020-09-22 12:51:03 +0800 CST

Python 3 中 b' ' 中涵盖的变量

  • 0

我确信这是一个相当菜鸟的问题,我用谷歌搜索过,找不到直接的答案,但我可能问错了……我正在尝试制作一个开箱即用的配置脚本,以及所有需要的问题回答存储在一个名为 pass.ini 的文件中。当我在填充我的文件时从getstr(使用curses)获取用户输入时,它们都有b'variable string'作为它们的值。当我尝试执行剥离命令时,我得到 b'riable strin'。当我做一个 str(variable) 时,它会遇到同样的问题。我看到 b'<variable_string>' 可以表示它是字节码而不是解码的标志。所以我尝试了一个解码命令,但失败了,因为'str object has no attribute'decode'我让它通过ConfigParser写出来,并作为file.write写到一个单独的文件中。现在一切都被注释掉了,

这是信息收集模块:

        wrapper(CommitChanges)
            curses.echo()
            stdscr.addstr(  8, 19,  config.CIP , curses.color_pair(3) )
            config.CIP = stdscr.getstr(  8, 19, 15)
            stdscr.addstr(  9, 19,  config.CSM , curses.color_pair(3) )
            config.CSM = stdscr.getstr( 9, 19, 15)
            stdscr.addstr( 10, 19,  config.CGW , curses.color_pair(3) )
            config.CGW = stdscr.getstr(10, 19, 15)
            stdscr.addstr( 11, 19,  config.CD1 , curses.color_pair(3) ) 
            config.CD1 = stdscr.getstr(11, 19, 15)
            stdscr.addstr( 12, 19,  config.CD2 , curses.color_pair(3) )
            config.CD2 = stdscr.getstr(12, 19, 15)
            stdscr.addstr( 13, 19,  config.CNTP, curses.color_pair(3) )
            config.CNTP = stdscr.getstr(13, 19, 15)
            stdscr.addstr( 16, 19,  config.CHN , curses.color_pair(3) )
            config.CHN = stdscr.getstr(16, 19, 15)
            stdscr.addstr( 14, 19,  config.CID , curses.color_pair(3) )
            config.CID = stdscr.getstr(14, 19, 15)
            stdscr.addstr( 15, 19,  config.CS , curses.color_pair(3) )
            config.CS = stdscr.getstr(15, 19, 15)


这是文件输出模块

def CommitChanges():
    MOP = "X"
    Config['Array=all']['PTLIP']        = a
    Config['Array=all']['PTLSM']        = config.CSM.decode('utf-8')
    Config['Array=all']['PTLGW']        = config.CGW.decode('utf-8')  
    Config['Array=all']['PTLD1']        = config.CD1.decode('utf-8')
    Config['Array=all']['PTLD2']        = config.CD2.decode('utf-8') 
    Config['Array=all']['PTLNTP']       = config.CNTP.decode('utf-8') 
    Config['Array=all']['PTLIF']        = config.CIFN.decode('utf-8') 
    Config['Array=all']['PTLHSTNM']     = config.CHN.decode('utf-8') 
    Config['Array=all']['PTLMOB']       = config.CMOB.decode('utf-8')
    Config['Array=all']['customerid']   = config.CID.decode('utf-8')
    Config['Array=all']['site']         = config.CS.decode('utf-8')
    with open('/opt/passp/pass.ini', 'w') as passini:
        Config.write(passini, space_around_delimiters=False)
    tpass= open('./pass.b', 'w')
    tpass.write("[Array=All]"+ "\n")
    tpass.write("ptlip="+ a + "\n")
    tpass.write("ptlsm="+ config.CSM.decode('utf-8') +"\n")
    tpass.write("ptlgw="+ config.CGW.decode('utf-8') + "\n")
    tpass.write("ptld1="+ config.CD1.decode('utf-8') + "\n")
        tpass.write("ptld2="+ config.CD2.decode('utf-8') + "\n")
    tpass.write("ptlntp="+ config.CNTPdecode('utf-8') + "\n")
    tpass.write("ptlif="+ config.CIFNdecode('utf-8') + "\n")
    tpass.write("ptldhstnm="+ config.CHNdecode('utf-8') + "\n")
    tpass.write("ptlmob="+ config.CMOBdecode('utf-8') + "\n")
    tpass.write("customerid="+ config.CIDdecode('utf-8') + "\n")
    tpass.write("site="+ config.CSdecode('utf-8') + "\n")
    #if Backupfiles():
    textchanges()
    return

这是 ConfigParser 创建的文件保存输出

[Array=all]
ptlip=b'123'
ptlsm=b'321'
ptlgw=b'111'
ptld1=b'222'
ptld2=b'333'
ptlntp=b'444'
ptlif=s19
ptlhstnm=b'555'
ptlmob=
customerid=b'666'
site=b'777'

当我进行直接写入时它完全匹配(它们来自两次不同的运行,但即使有空数据它也有包装器。

有趣的是,'ptlif' 是通过查找接口名称收集的,它不是由用户输入处理的,因此它必须是 config.XXXX 变量的存储方式。


[Array=All]
ptlip=b''
ptlsm=b''
ptlgw=b''
ptld1=b''
ptld2=b''
ptlntp=b''
ptlif=s19
ptldhstnm=b''
ptlmob=
customerid=b''
site=b''
linux python variables strings
  • 1 个回答
  • 69 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve