dictionary - Can two strings arguments be passed to a python dict() built in function? -
i have loop build dictionary from. part of code i'm having trouble both key , value strings. cannot convert ip variable string int nor float.
here method class i'm attempting build dictionary with. there loop elsewhere walking ip range i'm interested in feeding method parameter 'ip'.
def dictbuild(self,ip): s = pxssh.pxssh() s.force_password = true try: s.login(str(ip), 'root', 'password') s.sendline('hostname') # run command s.prompt() # match prompt out = s.before # print before prompt, returns byte object , need decode(utf-8) out = out.decode('utf-8') out = out.split() out = out[1] print(type(out)) #these type function give easy output of data types in traceback print(type(ip)) ipdict = dict(out=ip) ###getting stuck here on 2 string types. print(ipdict) s.logout() except (pxssh.exceptionpxssh, pexpect.eof) e: pass
instead of passing string want (it hostname of box) output this...
<class 'str'> <class 'str'> {'out': '10.20.234.3'}
the python documentation gives examples of key string , value int. using wrong?
https://docs.python.org/2/tutorial/datastructures.html
thanks in advance.
use dict literal instead:
ipdict = { out: ip }
calling dict()
in way passing named arguments; dict literals take expressions keys , expressions values.
Comments
Post a Comment