2013-04-01 59 views
0

我有一個json文件,我正在導入Python並試圖查找短語的出現。如果我試試這個:用python查找json中的文本

any(keyword in s for s in json_data) 

其中,keyword是我在尋找和json_data的事情是什麼,我使用了json.load()。即使關鍵字在json_data中,它也會返回false。下面是我如何索引到JSON:

json_data["thing1"]["thing2"][0]["thing3"] 

場[0] 0-16變化,我想的東西是thing3。即使關鍵字在json_data中,爲什麼我不能獲得True?

回答

0
any(keyword in s for s in json_data) 

正在尋找第一級字典,其中thing1位於。你必須看看包含thing3字典,這是一個索引

any(keyword in s for s in json_data["thing1"]["thing2"]) 

假設thing3keyword你正在尋找...

+0

嗯,有一個更多的部分給它。基本上我試圖找到一個循環json_data [「thing1」] [「thing2」] [0] [「thing3」],然後json_data [「thing1」] [「thing2」] [1] [ 「thing3」]等,其中thing3是關鍵字。 – blueintegral

+1

好的,在json_data [「thing1」] [「thing2」])中將其更改爲'any(s [「thing3」]中的關鍵字)'' – Ngenator