【发布时间】:2017-07-13 05:34:54
【问题描述】:
代码:
import json
import urllib
URL = 'www.xyz.com'
data = json.load(urllib.urlopen(url)
print data
[[{"Region":"Europe", "Details":[{"gender":"male", "name":"john", "age":"24", "status":"Ok"}, {"gender":"female", "name":"Rebecca", "age":"22", "status":"None"}], "country":"Germany"}], [{"Region":"Asia", "Details":[{"gender":"male", "name":"kim", "age":"27", "status":"None"}, {"gender":"male", "name":"jen", "age":"22", "status":"None"}], "country":"China"}]]
# here is what I have tried
for i in data:
for j in i:
for key in j.keys():
dicx = j[key]
for k in dicx:
if isinstance(k, dict) and k['status']=='None':
print (i['region'], k['name'])) #I wnant to store this value in a variable rather than printing.
#this is giving me the following output.
Europe, Rebecca
Asia, kim
Asia, jen
场景:根据上面的数据,我想检查所有“区域”(键)是否“状态”(键)是“Ok”(值)或“None”(值),如果“状态”( key) 是“None”(value) 那么它应该返回我那个特定“Region”(key) 的详细信息(value) 以及“name”(key)。
例如:- 期望的输出(参考上述数据)
Europe, Rebecca
Asia, (kim, jen)
任何帮助将不胜感激。
【问题讨论】:
-
我尝试使用循环对其进行迭代,但没有得到所需的输出。我得到与此类似的输出:Europe, Rebecca Asia, Kim Asia, Jen 我不想重复输出(因为我使用循环来迭代所以它是这样的)而且我无法将这些值存储到一个变量。
-
欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。 StackOverflow 不是编码或教程服务。
-
@jamesorc - 请将不起作用的代码发布到期望值和描述(如您的评论)
-
抱歉不完整部分,更新了上面的部分。
-
made in javascript你可以看看逻辑转换成python