转换模块

TIPCommon.transformation.add_prefix_to_dict

TIPCommon.transformation.add_prefix_to_dict(given_dict: dict, prefix: str) → dict

向给定字典的键添加前缀。

参数

参数
given_dict dict

要向其添加前缀的字典。

prefix str

应用于字典键的前缀。

返回值

添加了前缀的键的字典。

返回类型

dict

TIPCommon.transformation.add_prefix_to_dict_keys

TIPCommon.transformation.add_prefix_to_dict_keys(target_dict: dict, prefix: str) → dict

向给定字典的键添加前缀。

参数

参数
target_dict dict

要向其添加前缀的字典。

prefix str

应用于字典键的前缀。

返回值

添加了前缀的键的字典。

返回类型

dict

TIPCommon.transformation.adjust_to_csv

TIPCommon.transformation.adjust_to_csv(value)

调整值以使其适合纳入 CSV 文件。

参数
value Any

要调整的值。

返回值

调整后的价值。

返回类型

str

TIPCommon.transformation.construct_csv

TIPCommon.transformation.construct_csv(list_of_dicts)

根据字典列表构建 CSV。

参数
list_of_dicts list[dict]

要添加到 CSV 的字典列表。

返回值

采用 CSV 格式的列表。

返回类型

list[str]

TIPCommon.transformation.convert_comma_separated_to_list

TIPCommon.transformation.convert_comma_separated_to_list(comma_separated, delimiter=',')

将以英文逗号分隔的字符串转换为值列表。

参数
delimiter 用于解析字符串的分隔符。
默认值为 ','
comma_separated str

要转换的以英文逗号分隔的字符串。

返回值

值的列表。

返回类型

list

TIPCommon.transformation.convert_dict_to_json_result_dict

TIPCommon.transformation.convert_dict_to_json_result_dict(json_result, title_key='Entity', results_key='EntityResult')

key, value JSON 结果转换为 JSON 结果对象列表,并将实体 JSON 结果对象整理为一组格式。

dict 如何转换为 JSON 结果的示例如下:

{k1: v1, k2:v2, ...} =>
[
   {
       title_key: k1,
       result_key: v1
   },
   {
       title_key: k2,
       result_key: v1
   }
   ...
]
参数
json_result dict[str, Any] | str

key, value JSON 结果。

title_key str

每个键的名称所在的键。

result_key str

每个值的名称所对应的键。

示例

实体结果 JSON 格式的默认示例(其中 title_key='Entity'results_key='EntityResult')如下所示:

[
   {
       'Entity': 'key1 in json_result',
       'EntityResult: {
           json_result['key1']
       }
   },
   {
       'Entity': 'key2 in json_result',
       'EntityResult: {
           json_result['key2']
       }
   }
]

返回值

(list[dict[str, Any]]) 实体 JSON 结果对象列表

可能引发的错误

  • InternalJSONDecoderError - 如果 json_result 是字符串,但无法使用 json.loads() 解析为字典。
  • ValueError - 如果 json_result 不是 dict(如果需要,在从字符串加载后进行检查)。

TIPCommon.transformation.convert_list_to_comma_string

TIPCommon.transformation.convert_list_to_comma_string(values_list, delimiter=',')

将值列表转换为以英文逗号分隔的字符串。

参数
delimiter 字符串中要使用的分隔符。
默认值为 ','
values_list list

要转换的值的列表。

返回值

以英文逗号分隔的字符串。

返回类型

str

TIPCommon.transformation.dict_to_flat

TIPCommon.transformation.dict_to_flat(target_dict)

接收一个嵌套字典,并将其作为扁平字典返回。

参数
target_dict dict

要扁平化的字典。

返回值

扁平化字典。

返回类型

dict

TIPCommon.transformation.flat_dict_to_csv

TIPCommon.transformation.flat_dict_to_csv(flat_dict, property_header='Property', value_header='Value')

将扁平字典转换为 CSV 格式的字符串列表。

property_headervalue_header 参数用于自定义 CSV 标头。

参数
flat_dict dict

要转换为 CSV 格式的字典。

property_header str

属性列的标题。
默认值为 Property

value_header str

值列的标题。
默认值为 Value

返回值

采用 CSV 格式的字符串列表。

返回类型

list

TIPCommon.transformation.get_unicode

TIPCommon.transformation.get_unicode(value)

获取值的 Unicode。

参数
value Any

要转换为 Unicode 的值。

返回值

值的 Unicode 表示形式。

返回类型

unicode (unicode)

TIPCommon.transformation.removeprefix

TIPCommon.transformation.removeprefix(string: str, prefix: str) → str

针对 Python 3.9 及更高版本中存在的 str.removeprefix() 的自行实现。

如果字符串以前缀字符串开头,该方法会返回 string[len(prefix):]。否则,返回原始字符串的副本。

参数
string str

要移除前缀的字符串。

prefix str

要从字符串中移除的前缀。

返回值

生成的字符串。

TIPCommon.transformation.removesuffix

TIPCommon.transformation.removesuffix(string: str, suffix: str) → str

针对 Python 3.9 及更高版本中存在的 str.removesuffix() 的自行实现。

如果字符串以后缀字符串结尾,该方法会返回 string[:-len(prefix)]。否则,返回原始字符串的副本。

参数
string str

要移除后缀的字符串。

suffix str

要从字符串中移除的后缀。

返回值

生成的字符串。

TIPCommon.transformation.rename_dict_key

TIPCommon.transformation.rename_dict_key(a_dict: dict, current_key: Hashable,new_key: Hashable) → None

重命名字典中的键。

参数
a_dict dict

要重命名键的字典。

current_key Hashable

要重命名的 a_dict 中的键。

new_key Hashable

重命名后的键。

TIPCommon.transformation.string_to_multi_value

TIPCommon.transformation.string_to_multi_value(string_value, delimiter=',', only_unique=False)

将包含以英文逗号分隔的值列表的字符串转换为值列表。

参数
string_value str

要转换的字符串。

delimiter 可选

str

用于拆分字符串的分隔符。

默认值为 ','

only_unique 可选

bool

如果设置为 True,则参数仅包含返回列表中的唯一值。

默认值为 False

返回值

值的列表。

返回类型

list

需要更多帮助?从社区成员和 Google SecOps 专业人士那里获得解答。