Python 3.x 可以将两个字符串参数传递给python dict()内置函数吗?

Python 3.x 可以将两个字符串参数传递给python dict()内置函数吗?,python-3.x,dictionary,pexpect,Python 3.x,Dictionary,Pexpect,我有一个循环,我想用它来建立一个字典。我遇到问题的代码部分是,键和值都是字符串。我无法将IP变量返回字符串转换为int,也不能将其转换为float 下面是我试图使用的类中的方法来构建字典。在IP范围的其他地方有一个循环,我想输入方法参数'IP' def dictbuild(self,ip): s = pxssh.pxssh() s.force_password = True try: s.login(str(ip), 'root', 'password')

我有一个循环,我想用它来建立一个字典。我遇到问题的代码部分是,键和值都是字符串。我无法将IP变量返回字符串转换为int,也不能将其转换为float

下面是我试图使用的类中的方法来构建字典。在IP范围的其他地方有一个循环,我想输入方法参数'IP'

def dictbuild(self,ip):
    s = pxssh.pxssh()
    s.force_password = True
    try:
        s.login(str(ip), 'root', 'password')
        s.sendline('hostname')   # run a command
        s.prompt()             # match the prompt
        out = s.before        # print everything before the prompt, this returns a byte object and could need decode(utf-8)
        out = out.decode('utf-8')
        out = out.split()
        out = out[1]
        print(type(out)) #These type function give us an easy output of data types in the traceback
        print(type(ip))

        ipdict = dict(out=ip) ###Getting stuck here on two string types.
        print(ipdict)
        s.logout()
    except (pxssh.ExceptionPxssh, pexpect.EOF) as e:
        pass
不是传递我想要的字符串,而是框的主机名,我们得到如下输出

<class 'str'>
<class 'str'>
{'out': '10.20.234.3'}
python文档只给出了key作为string和value作为int的示例。我用错了吗


提前感谢。

请改用dict文本:

ipdict = { out: ip }

以这种方式调用dict只是传递命名参数;dict文字以表达式表示键,以表达式表示值。

self的输入是什么,ip表示给定的输出?这是一个类的方法,因此self是对象的当前实例,ip是一个类似于“10.20.234.3”的字符串。主机名类似于“P-2301”。使用python3.3进行的回溯。。。文件ipchk_v2.py,第53行ipdict=dict{out:ip}^语法错误:无效syntax@jonnymac:比较这两行。你的不应该有dict{在它里面。你是正确的,它确实是一个语法错误。好多了。