Re: Getting "ValueError: need more than 1 value to unpack" while trying to read a value from dictionary in python

2018-02-15 Thread MRAB

On 2018-02-15 11:25, Chris Warrick wrote:

On 15 February 2018 at 12:07, Sum J  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 :
 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


Re: Getting "ValueError: need more than 1 value to unpack" while trying to read a value from dictionary in python

2018-02-15 Thread Sum J
Thanks Chris :)

Its working now.

Regards,
Sumit

On Thu, Feb 15, 2018 at 4:55 PM, Chris Warrick  wrote:

> On 15 February 2018 at 12:07, Sum J  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 :
> >  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 = {}
> for i in s.split('\n'):
> try:
> key, val = i.split(":")
> d[key.strip()] = val.strip()
> except ValueError:
> print "no key:value pair found in", i
>
>
> (PS. please switch to Python 3)
>
> --
> Chris Warrick 
> PGP: 5EAAEA16
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Getting "ValueError: need more than 1 value to unpack" while trying to read a value from dictionary in python

2018-02-15 Thread Chris Warrick
On 15 February 2018 at 12:07, Sum J  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 :
>  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 = {}
for i in s.split('\n'):
try:
key, val = i.split(":")
d[key.strip()] = val.strip()
except ValueError:
print "no key:value pair found in", i


(PS. please switch to Python 3)

-- 
Chris Warrick 
PGP: 5EAAEA16
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Getting "ValueError: need more than 1 value to unpack" while trying to read a value from dictionary in python

2018-02-15 Thread Prahallad Achar
IPv4 address configured by DHCP

It doesn't find key and value for above string.

Solution.. Use one complete string per  line

On 15 Feb 2018 4:41 pm, "Sum J"  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 :
 key, val = i.split(":")
ValueError: need more than 1 value to unpack
--
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Getting "ValueError: need more than 1 value to unpack" while trying to read a value from dictionary in python

2018-02-15 Thread Sum J
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 :
 key, val = i.split(":")
ValueError: need more than 1 value to unpack
-- 
https://mail.python.org/mailman/listinfo/python-list