我是python的新手,我需要一个循环的帮助。这是代码:
def show_result(self):
listed = self.listed
res = {}
for key in listed:
query_result = 0
is_listed = 0
rbl = key
if not listed[key].get('ERROR'):
query_result = "success"
if listed[key]['LISTED']:
is_listed = 1
else:
is_listed = 0
else:
query_result = "error"
pass
res[key] = [ ['rbl', rbl], ['host', listed['SEARCH_HOST']], ['lookup', query_result], ['is_listed', is_listed] ]
return res
try:
con = db.connect(db_server, db_user, db_password, db_name)
cur = con.cursor()
#hosts = json array with Hosts and IP's
for host in hosts:
searcher = checkHost(host, rbls, report)
result = searcher.show_result()
#
# Need to loop through result to get rbl, host, lookup and is_listed variables
# to be able to execute the query and commit it later when loop is finished
#
cur.execute("INSERT INTO checks_reports_df8 (`rbl`, `host`, `lookup`, `is_listed`) VALUES(%s, %s, '%s')", (rbl, host, lookup, is_listed))
con.commit()
except db.Error, e:
if con:
con.rollback()
finally:
if con:
con.close()
如果我从searcher.show_result()得到pprint结果,这就是我得到的:
{u'0spam-killlist.fusionzero.com': [['rbl', u'0spam-killlist.fusionzero.com'],
['host', u'127.0.0.2'],
['lookup', 'success'],
['is_listed', 0]],
u'zen.spamhaus.org': [['rbl', u'zen.spamhaus.org'],
['host', u'127.0.0.2'],
['lookup', 'success'],
['is_listed', 1]]}
我的问题是我不知道如何遍历searcher.show_result()的结果。如果我的方法有误,可以更改searcher.show_result()返回的结果。