我有一个json数据,如下所示,我在每个字段中都有一个uri要匹配。 如果找到匹配项,则不要进行[fields]的下一次迭代。 概念是用户在搜索后单击了URL。 如果用户单击第三个URL,则收集第1,第2,第3个数据值。不要追求第4个值。
如果用户单击了第5个网址,则收集第1至第5个字段uri并退出该json对象。 采取一个新的json对象并执行相同的过程。
[document]-> [fields1] -> [uri]
[document]-> [fields2] -> [uri]
[document]-> [fields3] -> [uri]
.....
.. till 20-30 times.
I have written below code, but the above logic is not working. Kindly help on this.
uri='http://abcd.com/123.html'
print(uri)
for index_srch_log,row_srch_log in df_search_log_mongo.iterrows():
RESPONSE = row_srch_log['RESPONSE']
json_response = json.loads(RESPONSE)
if 'documents' in json_response:
field_data=json_response['documents']
for row_resp_list in field_data:
print('uri:',row_resp_list['fields']['uri'])
match_found=False
for i in row_resp_list['fields']['uri']:
print('i',i)
if uri == i:
print('yes matched')
match_found=True
break
print('found')
else:
print('not matched')
match_found=False
if match_found==True:
break
输出:
uri: ['http://abcddsc779072.html']
i value: http://abcddsc779072.html
not matched
uri: ['http://abcddsc932618.html']
i value: http://abcddsc932618.html
yes matched
-应该在此处停止并从DF获取下一个响应对象。 -但它再次继续下一个[fields]数据。
uri: ['http://abcddsc988555.html']
i value: http://abcddsc988555.html
not matched
uri: ['http://abcddsc1094909.html']
i value: http://abcddsc1094909.html
not matched
答案 0 :(得分:2)
您没有打破外部循环。考虑以下更改:
if 'documents' in json_response:
field_data=json_response['documents']
for row_resp_list in field_data:
print('uri:',row_resp_list['fields']['uri'])
match_found=False
for i in row_resp_list['fields']['uri']:
print('i',i)
if uri == i:
print('yes matched')
match_found=True
break
else:
print('not matched')
if match_found:
break