| 1 |
:orphan: |
| 2 |
|
| 3 |
grep is a more user friendly interface for DeepSearch. It takes exactly the same arguments as DeepSearch except that you pipe the object into it instead of passing it as a parameter. |
| 4 |
|
| 5 |
It works just like grep in linux shell! |
| 6 |
|
| 7 |
**Parameters** |
| 8 |
|
| 9 |
item : The item to search for |
| 10 |
|
| 11 |
verbose_level : int >= 0, default = 1. |
| 12 |
Verbose level one shows the paths of found items. |
| 13 |
Verbose level 2 shows the path and value of the found items. |
| 14 |
|
| 15 |
exclude_paths: list, default = None. |
| 16 |
List of paths to exclude from the report. |
| 17 |
|
| 18 |
exclude_types: list, default = None. |
| 19 |
List of object types to exclude from the report. |
| 20 |
|
| 21 |
case_sensitive: Boolean, default = False |
| 22 |
|
| 23 |
match_string: Boolean, default = False |
| 24 |
If True, the value of the object or its children have to exactly match the item. |
| 25 |
If False, the value of the item can be a part of the value of the object or its children |
| 26 |
|
| 27 |
use_regexp: Boolean, default = False |
| 28 |
|
| 29 |
strict_checking: Boolean, default = True |
| 30 |
If True, it will check the type of the object to match, so when searching for '1234', |
| 31 |
it will NOT match the int 1234. Currently this only affects the numeric values searching. |
| 32 |
|
| 33 |
|
| 34 |
**Examples** |
| 35 |
|
| 36 |
Importing |
| 37 |
>>> from deepdiff import grep |
| 38 |
>>> from pprint import pprint |
| 39 |
|
| 40 |
Search in list for string |
| 41 |
>>> obj = ["long somewhere", "string", 0, "somewhere great!"] |
| 42 |
>>> item = "somewhere" |
| 43 |
>>> ds = obj | grep(item) |
| 44 |
>>> print(ds) |
| 45 |
{'matched_values': ['root[0]', 'root[3]']} |
| 46 |
|
| 47 |
Search in nested data for string |
| 48 |
>>> obj = ["something somewhere", {"long": "somewhere", "string": 2, 0: 0, "somewhere": "around"}] |
| 49 |
>>> item = "somewhere" |
| 50 |
>>> ds = obj | grep(item, verbose_level=2) |
| 51 |
>>> pprint(ds, indent=2) |
| 52 |
{ 'matched_paths': {"root[1]['somewhere']": 'around'}, |
| 53 |
'matched_values': { 'root[0]': 'something somewhere', |
| 54 |
"root[1]['long']": 'somewhere'}} |
| 55 |
|
| 56 |
You can also use regular expressions |
| 57 |
>>> obj = ["something here", {"long": "somewhere", "someone": 2, 0: 0, "somewhere": "around"}] |
| 58 |
>>> ds = obj | grep("some.*", use_regexp=True) |
| 59 |
>>> pprint(ds, indent=2) |
| 60 |
{ 'matched_paths': ["root[1]['someone']", "root[1]['somewhere']"], |
| 61 |
'matched_values': ['root[0]', "root[1]['long']"]} |
| 62 |
|
| 63 |
|
| 64 |
Change strict_checking to False to match numbers in strings and vice versa: |
| 65 |
>>> obj = {"long": "somewhere", "num": 1123456, 0: 0, "somewhere": "around"} |
| 66 |
>>> item = "1234" |
| 67 |
>>> result = {"matched_values": {"root['num']"}} |
| 68 |
>>> ds = obj | grep(item, verbose_level=1, use_regexp=True) |
| 69 |
>>> pprint(ds) |
| 70 |
{} |
| 71 |
>>> |
| 72 |
>>> ds = obj | grep(item, verbose_level=1, use_regexp=True, strict_checking=False) |
| 73 |
>>> pprint(ds) |
| 74 |
{'matched_values': ["root['num']"]} |