轉換模組
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
|
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– ifjson_resultis a string and couldn't be parsed to dictionary usingjson.loads().ValueError- if thejson_resultisn't adict(the check is done after loading it from a string, if needed).
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_header 和 value_header 引數用於自訂 CSV 標頭。
| 參數 | |
|---|---|
flat_dict |
dict要轉換為 CSV 格式的字典。 |
property_header |
str屬性資料欄的標頭。
|
value_header |
str值資料欄的標頭。
|
傳回
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要重新命名金鑰的 dict。 |
current_key |
Hashable要重新命名的 |
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 |
選用
用來分割字串的分隔符號。 預設值為 |
only_unique |
選用
如果設為 預設值為 |
傳回
值的清單。
傳回類型
list
還有其他問題嗎?向社群成員和 Google SecOps 專業人員尋求答案。