On 2018-02-15 11:25, Chris Warrick wrote:
On 15 February 2018 at 12:07, Sum J <sjsum...@gmail.com> wrote:
Below is my code. Here I want to read the "ip address" from s
s= '''
Power On Enabled = On
State: connected
Radio Module: Unknown
noise: -097
signalStrength: -046
ip address: 192.168.75.147
subnet mask: 255.255.255.0
IPv4 address configured by DHCP
Mac Addr: ac:e2:d3:32:00:5a
Mode: infrastrastructure
ssid: Cloudlab
Channel: 1
Regulatory: World Safe
Authencation: WPA2/PSK
Encryption: AES or TKIP
'''
s = s.replace("=",":")
# s = s.strip()
print s
d = {}
for i in s:
key, val = i.split(":")
d[key] = val.strip()
print d
print d["ip address"]
Getting below error :
<module> key, val = i.split(":")
ValueError: need more than 1 value to unpack
--
https://mail.python.org/mailman/listinfo/python-list
If you iterate over a string, you are iterating over individual
characters. Instead, you need to split it into lines, first stripping
whitespace (starts and ends with an empty line).
s = s.strip().replace("=",":")
print s
d = {}
I'd use .splitlines instead:
for i in s.split('\n'):
try:
.partition works better here. You should note that the "Mac Addr" line
has multiple colons, most of which are part of the value!
key, val = i.split(":")
d[key.strip()] = val.strip()
except ValueError:
print "no key:value pair found in", i
s = s.replace("=",":")
d = {}
for line in s.splitlines():
key, sep, val = line.partition(":")
if sep:
d[key.strip()] = val.strip()
else:
print "no key:value pair found in", line
>
> (PS. please switch to Python 3)
> +1
--
https://mail.python.org/mailman/listinfo/python-list