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

Chris Warrick kwpolska at gmail.com
Thu Feb 15 06:25:14 EST 2018


On 15 February 2018 at 12:07, Sum J <sjsumitj at 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 = {}
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 <https://chriswarrick.com/>
PGP: 5EAAEA16



More information about the Python-list mailing list