[[+content_image]]
V
V
Vitaly2015-11-13 13:47:49
Python
Vitaly, 2015-11-13 13:47:49

Why does Python swear in a loop?

All the best!
Help me figure it out... there is an answ
variable that is equal to:

[[u'!re', {u'=invalid': u'false', u'=disabled': u'false', u'=.id': u'*2', u'=interface': u'br12-staff', u'=dynamic': u'false', u'=actual-interface': u'br12-staff', u'=comment': u'in', u'=network': u'10.10.12.0', u'=address': u'10.10.12.217/24'}], [u'!re', {u'=invalid': u'true', u'=disabled': u'true', u'=.id': u'*3', u'=interface': u'ether1-gateway', u'=dynamic': u'false', u'=actual-interface': u'ether1-gateway', u'=comment': u'RouterOS Upgrade', u'=network': u'172.16.0.0', u'=address': u'172.16.0.2/24'}], [u'!done', {}]]

if I extract data from array and dictionary manually, i.e.:
print answ[0][1][u'=address']
then everything is fine, but when I want to do it through a loop:
for x in answ:
    print x[1]['=address']

or
for x in answ:
    print x[1][u'=address']

I get the result, but I also get the error: print x[1][u'=address'] KeyError: u'=address'
How can I get rid of this error?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
angru, 2015-11-13
@Scorpiored88

https://docs.python.org/3/library/stdtypes.html#di... you have [u'!done', {}]
in your list of lists , the second element is an empty dictionary, respectively, in it no '=address' key

V
Vladimir Martyanov, 2015-11-13
@vilgeforce

You need to figure out what you have in the x variable at each iteration of the loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question