Python讀取json檔後找尋符合條件的字串並輸出所屬group
例如需要找到SmoothScroll這個字串並輸出為 group 1,目前有試過使用
for F in data:
if F=='SmoothScroll':
print('Group 1')
break
else:
print("no")!
但無法達到所要的需求,有進行爬文但沒有搜尋到方法,還請各位大大指點
以下是json檔
{'group 1': [{'index': 8,
'API name': 'RegOpenKey',
'params': {'hKey': 'HKEY_CURRENT_USER\\Control Panel\\Desktop'},
'action': 'Read'},
{'index': 9,
'API name': 'RegQueryValue',
'params': {'hKey': 'HKEY_CURRENT_USER\\Control Panel\\Desktop\\SmoothScroll'},
'action': 'Read'},
{'index': 10,
'API name': 'RegCloseKey',
'params': {'hKey': 'HKEY_CURRENT_USER\\Control Panel\\Desktop'},
'action': 'Close'},
{'index': 54,
'API name': 'RegOpenKey',
'params': {'hKey': 'HKEY_CURRENT_USER\\Control Panel\\Desktop'},
'action': 'Read'},
{'index': 55,
'API name': 'RegQueryValue',
'params': {'hKey': 'HKEY_CURRENT_USER\\Control Panel\\Desktop\\SmoothScroll'},
'action': 'Read'},
{'index': 56,
'API name': 'RegCloseKey',
'params': {'hKey': 'HKEY_CURRENT_USER\\Control Panel\\Desktop'},
'action': 'Close'}]}
import json
json_str = r"""
{
"group 1": [{
"index": 8,
"API name": "RegOpenKey",
"params": {
"hKey": "HKEY_CURRENT_USER\\Control Panel\\Desktop"
},
"action": "Read"
},
{
"index": 9,
"API name": "RegQueryValue",
"params": {
"hKey": "HKEY_CURRENT_USER\\Control Panel\\Desktop\\SmoothScroll"
},
"action": "Read"
},
{
"index": 10,
"API name": "RegCloseKey",
"params": {
"hKey": "HKEY_CURRENT_USER\\Control Panel\\Desktop"
},
"action": "Close"
},
{
"index": 54,
"API name": "RegOpenKey",
"params": {
"hKey": "HKEY_CURRENT_USER\\Control Panel\\Desktop"
},
"action": "Read"
},
{
"index": 55,
"API name": "RegQueryValue",
"params": {
"hKey": "HKEY_CURRENT_USER\\Control Panel\\Desktop\\SmoothScroll"
},
"action": "Read"
},
{
"index": 56,
"API name": "RegCloseKey",
"params": {
"hKey": "HKEY_CURRENT_USER\\Control Panel\\Desktop"
},
"action": "Close"
}
],
"group 2": [{
"index": 8,
"API name": "RegOpenKey",
"params": {
"hKey": "HKEY_CURRENT_USER\\Control Panel\\Desktop"
},
"action": "Read"
}]
}"""
data = json.loads(json_str)
def haveKeyWord(l, keyword):
_ = list(filter(lambda x: keyword in x['params']['hKey'].split("\\"), l))
return True if _ else False
result = dict(filter(lambda x:haveKeyWord(x[1], "SmoothScroll"), data.items()))
print(result.keys())
幫你改了JSON,並且多加了一組測試的 group 2