Python二进制和正则字符串混淆

2024-09-24 22:20:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我确信这是一个相当愚蠢的问题,我已经在谷歌上搜索过,找不到一个直接的答案,但我可能问错了。。。我正在尝试制作一个现成的配置脚本,所有需要回答的问题都存储在一个名为pass.ini的文件中。当我从getstr(使用诅咒)获取用户输入并填充我的文件时,它们的值都是b'variable string'。当我试着执行脱衣命令时,我得到了“可变的strin”。当我使用str(变量)时,它也会遇到同样的问题。我看到了b'<;变量字符串>;'可能是一个信号,表明它是字节码而不是解码的。所以我尝试了一个decode命令,但失败了,因为'str object没有属性'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''

Tags: 文件configallarraycursesutfwritecolor
1条回答
网友
1楼 · 发布于 2024-09-24 22:20:18

好的,我知道我的问题出在哪里了

ncurses getstr返回一个二进制值,我将它发送到一个字符串文本变量

我创建了一组新的变量,将它们设置为

cipb = b'foo'
cipb = stdscr.getstr( y, x, len )
config.CIP = cipb.decode()

这就解决了问题。希望这可以帮助其他人解决类似的问题

相关问题 更多 >