用python写一个去除文本隐私信息的脚本

import re

email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
phone_pattern = r'\b\d{10,13}\b'
name_pattern = r'\b[A-Za-z ]+\b'  # 可根据需要自行修改,这里只是一个示例
id_pattern = r'\b\d{15}(\d{2}[0-9X])?\b'
workid_pattern = r'\b[A-Za-z0-9]{5,10}\b'  # 修改以匹配含英文大小写的工号
qq_pattern = r'\b[1-9][0-9]{4,}\b'

privacy_patterns = [email_pattern, phone_pattern, name_pattern, id_pattern, workid_pattern, qq_pattern]

def mask_privacy(text, patterns):
    for pattern in patterns:
        # Find all the matches
        matches = re.findall(pattern, text)
        for match in matches:
            # Replace each character in the match with '*'
            masked = '*' * len(match)
            # Replace the match in the text with the masked string
            text = text.replace(match, masked)
    return text

with open("input.txt", "rt", encoding="utf-8") as input_file:
    text = input_file.read()

masked_text = mask_privacy(text, privacy_patterns)

with open("output.txt", "wt", encoding="utf-8") as output_file:
    output_file.write(masked_text)

姓名:张三,张三没有替换掉

name_pattern = r'\b[A-Za-z]+(?:\s[A-Za-z]+)*\b|\b[\u4e00-\u9fa5]{2,4}\b'

这里限制了2-4个中文字符,但是其他的也被匹配掉了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值