我正在使用DeepSearch来找到一个项目,并且可行。我想要的只是返回['members']
的索引ID。在这种情况下,ID为[1]
。有关如何执行此操作的任何想法?
任何帮助将不胜感激,我仍在学习Python,因此尝试解决问题。
代码:
from deepdiff import grep
obj = response.json()
item = ".rep"
ds = obj | grep(item, verbose_level=2)
print(ds)
返回:
matched_values': {"root['data'][1]['members'][0]['address']"}}
可以使用正则表达式过滤掉它吗? \ D会过滤为1和0。我如何删除0而只留下1?
答案 0 :(得分:0)
我用以下代码解决了这个问题:
import requests
from deepdiff import grep
import re
response = s.get(url)
obj = response.json()
item = ".rep"
ds = obj | grep(item)
print(ds) # {'matched_values': {"root['data'][8]['members'][0]['address']"}}
aa = re.search(r"\d", str(ds))
print(aa) # <re.Match object; span=(34, 35), match='8'>
if aa is not None:
bb = re.findall(r'(\d)', str(ds))
print(bb) # ['8', '0']
if len(bb) == 3: # The list may be three characters total
bb.pop(2) # The third character is not needed
print(bb)
bb = ''.join(str(i) for i in bb[:2]) # convert the list to a string
print(bb)
else:
bb = re.search(r'(\d)', str(ds)) # find only the first int as that's all that's needed
print(format(bb.group(0))) # 8
else:
print('no matches found')