Google DLP掩码功能未忽略指定字符问题求助
解决Google DLP掩码时忽略指定字符无效的问题
问题根源
- 未配置忽略字符:代码中定义了
charactersToIgnore参数,但未将其添加到character_mask_config的characters_to_ignore字段中,导致DLP无法识别需要保留的字符。 - 掩码长度设置过大:
number_to_mask=25超过了目标邮箱aabernathy@example.com的总长度(22个字符),会强制掩码整个邮箱内容,即使配置了忽略字符也无法生效。
修正后的代码
from typing import List import google.cloud.dlp_v2 def deidentify_with_mask( project: str, input_str: str, info_types: List[str], masking_character: str = None, number_to_mask: int = 0, charactersToIgnore : list[str]=None ) -> None: # Instantiate a client dlp = google.cloud.dlp_v2.DlpServiceClient() # Convert the project id into a full resource id. parent = f"projects/{project}" # Construct inspect configuration dictionary inspect_config = {"info_types": [{"name": info_type} for info_type in info_types]} # 构建字符忽略配置(转换为DLP要求的格式) ignore_config = [] if charactersToIgnore: ignore_config = [{"value": char} for char in charactersToIgnore] # Construct deidentify configuration dictionary deidentify_config = { "info_type_transformations": { "transformations": [ { "primitive_transformation": { "character_mask_config": { "masking_character": masking_character, "number_to_mask": number_to_mask, "characters_to_ignore": ignore_config # 添加忽略字符配置 } } } ] } } # Construct item item = {"value": input_str} # Call the API response = dlp.deidentify_content( request={ "parent": parent, "deidentify_config": deidentify_config, "inspect_config": inspect_config, "item": item, } ) # Print out the results. print(response.item.value) # Replace with your project ID project_id = "sandydev" # Specify the input string, info types, masking character, and number to mask input_string = "My name is Alicia Abernathy, and my email address is aabernathy@example.com." info_types = ["EMAIL_ADDRESS"] masking_character = "*" number_to_mask = 0 # 设置为0,掩码除忽略字符外的所有内容 # Call the deidentify_with_mask function deidentify_with_mask( project=project_id, input_str=input_string, info_types=info_types, masking_character=masking_character, number_to_mask=number_to_mask, charactersToIgnore=["@", "."] # 添加需要保留的.字符,符合预期输出格式 )
修正说明
- 添加忽略字符配置:将
charactersToIgnore转换为DLP要求的字典列表格式,传入character_mask_config的characters_to_ignore字段,确保指定字符(如@、.)被保留。 - 调整掩码长度:将
number_to_mask设为0,DLP会自动掩码除了忽略字符之外的所有字符,完美保留邮箱的结构。 - 扩展忽略字符:根据预期输出,添加
.到忽略字符列表中,确保邮箱的域名部分结构也被保留。
测试输出
运行修正后的代码,输出将符合预期:
My name is Alicia Abernathy, and my email address is **********@*********.***.
内容的提问来源于stack exchange,提问作者Sandeep Mohanty
相关产品推荐
相关产品推荐

