如何在具有嵌套键和值的Json文件中搜索某个关键字? [英] How can I search for a certain keyword in a Json file that has nested keys and values?
问题描述
我是python的初学者,我正在尝试在Json文件中搜索特定的关键字.我一直在阅读字典和列表在python中的工作方式,我偶然发现了这一点:
I am somewhat a beginner in python and I am trying to search for a specific keyword in a Json file. I have been reading up on how dictionaries and lists work in python and I came across this:
complex_list = [["a",["b",["c","x"]]],42]
complex_list[0][1]
output: ['b', ['c', 'x']]
complex_list[0][1][1][0]
output: 'c'
据我了解,在complex_ list [0] [1]
中, [0]
是整个括号 [
,]
和 [1]
访问括号的第二部分: [,[this one]]
.
As I understand, in complex_list[0][1]
, the [0]
is the entire bracket [
, ]
, and [1]
accesses the second part of the bracket: [, [this one] ]
.
现在,这个代码: complex_list = [["a",["b",["c","x"]],42]
,在列表中有2个正确的元素?a,b,c和x属于一个集合,而42属于第二个集合.我不知道该怎么解释: complex_list [0] [1] [1] [0]
访问'c'
.
Now, this one: complex_list = [["a",["b",["c","x"]]],42]
, has 2 elements within the list correct? a, b, c ,and x belong to one set and 42 belongs to the second set. I don't know how to interpret this: complex_list[0][1][1][0]
to access 'c'
.
有人可以将其分解吗?我之所以这样问,是因为我认为这是解决以下我要解释的问题所需要的.
Could someone break it down please? I ask this because I think this is what I need to use to solve the problem I explain below.
这是我目前正在使用的文件中的一小部分样本:
This is a small sample from the file I am working with at the moment:
{ (white)
"results": [
{ (black)
"Fruit": "Apple",
"Nested fruit": [
"Orange"
],
"Title1": "Some text",
"Contents": { (yellow)
"Name 1": [
"John Smith"
],
"Name 2": [
"Tyler"
],
"Name 3": [
"Bob",
"Rob"
],
"Name 4": [
"Linda"
],
"Name 5": [
"Mark",
"Matt"
],
"Some boolean": [
true
]
}, (yellow)
"More stuff": "More random text",
"Confusing": [
{ (red)
"Some info": "456",
"Info I want": "849456"
} (red)
],
"Not important": [
{ (blue)
"random text": "bla",
"random text2": "bla bla"
} (blue)
],
"Not important 2": "000",
"Not important3": [
"whatever",
"whatever"
],
"Not important 4": "16",
"Not important 5": "0058"
} (black)
]
} (white)
我在括号中的相应花括号旁边放置了颜色,以便于区分.通过网上的一些示例,我发现:
I have put colors in parenthesis next to their corresponding curly braces so that it is easy to distinguish. Following some examples online, I found:
import json
with open('searchingKeywords.json') as f:
data = json.load(f)
print(data.keys())
for k in data:
for v in data[k]:
if 'More stuff' in v:
print("yes")
打印:
dict_keys(['results'])
yes
只有1个键,但是内容呢?这不是结果中的另一个关键吗?我感到很困惑.我感兴趣的是令人困惑"中的我想要的信息".如果包含关键字我想要的信息",如何在许多嵌套的内容中进行搜索?最初,我尝试逐行读取-将Json文件解析为Python对象后,然后查看是否在每行中都找到了关键字我想要的信息",但我一直遇到错误.此外,我正在使用的文件很大,我想要的信息"的嵌套方式可能有所不同.
There is only 1 key, but what about Contents? Isn't that another key within results? I am so confused. What I am interested in is "info I want" inside "Confusing". How do I search inside so many nested things if keyword "Info I want" is contained? Initially, I tried reading line by line-- once I parsed the Json file into a Python object-- and then see if a keyword "Info I want" is found in each line but I kept getting errors. Additionally, the file I am working with is huge and "Info I want" may be nested differently.
推荐答案
As mentioned in the comments, the not accepted answer in the linked question works perfectly fine for you case:
data = {
"results": [
{
"Fruit": "Apple",
"Nested fruit": [
"Orange"
],
"Title1": "Some text",
"Contents": {
"Name 1": [
"John Smith"
],
"Name 2": [
"Tyler"
],
"Name 3": [
"Bob",
"Rob"
],
"Name 4": [
"Linda"
],
"Name 5": [
"Mark",
"Matt"
],
"Some boolean": [
True
]
},
"More stuff": "More random text",
"Confusing": [
{
"Some info": "456",
"Info I want": "849456"
}
],
"Not important": [
{
"random text": "bla",
"random text2": "bla bla"
}
],
"Not important 2": "000",
"Not important3": [
"whatever",
"whatever"
],
"Not important 4": "16",
"Not important 5": "0058"
}
]
}
def item_generator(json_input, lookup_key):
if isinstance(json_input, dict):
for k, v in json_input.items():
if k == lookup_key:
yield v
else:
yield from item_generator(v, lookup_key)
elif isinstance(json_input, list):
for item in json_input:
yield from item_generator(item, lookup_key)
res = item_generator(data, 'More stuff')
print([x for x in res])
res = item_generator(data, 'Info I want')
print([x for x in res])
输出:
['More random text']
['849456']
这篇关于如何在具有嵌套键和值的Json文件中搜索某个关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!