一个函数返回String s: s ='开机启用:开\ nsignalStrength:-050 \ nip地址:192.168.75.123 \ n'
我想在上面的字符串s中读取ip地址的值。 使用下面的代码: / *代码* /
s = 'Power On Enabled: On\nsignalStrength: -050\nip address:
192.168.75.123\n'
ipdict = {}
newstr = s.replace("\n",",")
newdict = ipdict.update(newstr)
ip = newdict.get('ip address')
print ip
得到以下错误: "字典更新序列元素#0的长度为1; 2是必需的"
答案 0 :(得分:0)
s = 'Power On Enabled: On\nsignalStrength: -050\nip address: 192.168.75.123\n'
s = s.strip().split("\n")
d = {}
for i in s:
key, val = i.split(":")
d[key] = val.strip()
print d
print d["ip address"]
<强>输出:强>
{'signalStrength': '-050', 'Power On Enabled': 'On', 'ip address': '192.168.75.123'}
192.168.75.123