github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/md/kv_split.en.md (about)

     1  ### `kv_split()` {#fn-kv_split}
     2  
     3  Function prototype: `fn kv_split(key, field_split_pattern = " ", value_split_pattern = "=", trim_key = "", trim_value = "", include_keys = [], prefix = "") -> bool`
     4  
     5  Function description: extract all key-value pairs from a string
     6  
     7  Function parameters:
     8  
     9  - `key`: key name
    10  - `include_keys`: list of key names, only extract the keys in the list; **the default value is [], do not extract any key**
    11  - `field_split_pattern`: string splitting, a regular expression used to extract all key-value pairs; the default value is " "
    12  - `value_split_pattern`: used to split the key and value from the key-value pair string, non-recursive; the default value is "="
    13  - `trim_key`: delete all the specified characters leading and trailing the extracted key; the default value is ""
    14  - `trim_value`: remove all leading and trailing characters from the extracted value; the default value is ""
    15  - `prefix`: add prefix to all keys
    16  
    17  Example:
    18  
    19  
    20  ```python
    21  # input: "a=1, b=2 c=3"
    22  kv_split(_)
    23   
    24  '''output:
    25  {
    26    "message": "a=1, b=2 c=3",
    27    "status": "unknown",
    28    "time": 1679558730846377132
    29  }
    30  '''
    31  ```
    32  
    33  ```python
    34  # input: "a=1, b=2 c=3"
    35  kv_split(_, include_keys=["a", "c", "b"])
    36   
    37  '''output:
    38  {
    39    "a": "1,",
    40    "b": "2",
    41    "c": "3",
    42    "message": "a=1 b=2 c=3",
    43    "status": "unknown",
    44    "time": 1678087119072769560
    45  }
    46  '''
    47  ```
    48  
    49  ```python
    50  # input: "a=1, b=2 c=3"
    51  kv_split(_, trim_value=",", include_keys=["a", "c", "b"])
    52  
    53  '''output:
    54  {
    55    "a": "1",
    56    "b": "2",
    57    "c": "3",
    58    "message": "a=1, b=2 c=3",
    59    "status": "unknown",
    60    "time": 1678087173651846101
    61  }
    62  '''
    63  ```
    64  
    65  
    66  ```python
    67  # input: "a=1, b=2 c=3"
    68  kv_split(_, trim_value=",", include_keys=["a", "c"])
    69  
    70  '''output:
    71  {
    72    "a": "1",
    73    "c": "3",
    74    "message": "a=1, b=2 c=3",
    75    "status": "unknown",
    76    "time": 1678087514906492912
    77  }
    78  '''
    79  ```
    80  
    81  ```python
    82  # input: "a::1,+b::2+c::3" 
    83  kv_split(_, field_split_pattern="\\+", value_split_pattern="[:]{2}",
    84      prefix="with_prefix_",trim_value=",", trim_key="a", include_keys=["a", "b", "c"])
    85  
    86  '''output:
    87  {
    88    "message": "a::1,+b::2+c::3",
    89    "status": "unknown",
    90    "time": 1678087473255241547,
    91    "with_prefix_b": "2",
    92    "with_prefix_c": "3"
    93  }
    94  '''
    95  ```