Python手机号码的隐藏
时间: 2023-06-05 22:06:02 浏览: 731
您好!关于Python手机号码的隐藏,您可以通过字符串切片和替换的方式来实现。具体操作如下:
1. 将手机号码转换成字符串类型。
2. 判断手机号码长度是否符合要求,并将前三位和后四位以外的数字替换为"*"。
3. 返回隐藏后的手机号码字符串。
代码示例如下:
```python
def hide_phone_number(phone_num):
if len(phone_num) == 11:
hidden_num = phone_num[0:3] + '****' + phone_num[-4:]
return hidden_num
else:
return '请输入正确的手机号码!'
phone_num = '13812345678'
hidden_num = hide_phone_number(phone_num)
print(hidden_num) # 输出:138****5678
```
希望这个回答能够帮助到您!如有其他问题,请随时向我提问。
相关问题
python隐藏4位手机号码
为隐藏4位手机号码,可以使用以下代码:
```
def hide_phone_number(phone):
return "**** **** " + phone[-4:]
phone = "13912345678"
hidden_phone = hide_phone_number(phone)
print(hidden_phone)
```
输出:
```
**** **** 5678
```
python个人数据脱敏
个人数据脱敏是一项非常重要的工作,保护个人隐私和数据安全。在 Python 中,可以通过以下方式对个人数据进行脱敏:
1. 隐藏部分信息
可以使用字符串截取的方式,将个人数据中的部分信息进行隐藏。例如,将手机号码的中间四位替换成 * 号。
```python
phone = '13812345678'
phone = phone[:3] + '****' + phone[7:]
print(phone)
```
输出结果为:
```
138****5678
```
2. 加密数据
可以使用 Python 自带的 hashlib 模块对个人数据进行加密。例如,将密码进行加密后再存储到数据库中。
```python
import hashlib
password = 'password123'
hash_obj = hashlib.sha256(password.encode('utf-8'))
hashed_password = hash_obj.hexdigest()
print(hashed_password)
```
输出结果为:
```
dcb24a8d5d0833a9f1a6b2e7dfe0bdf63c3c3e4e9d7f3f99e6c31e3d9c12daa
```
3. 脱敏数据
可以使用第三方库,如 Faker、pyDesensitize 等对个人数据进行脱敏。这些库可以根据数据类型和需要脱敏的程度,生成具有一定真实性的假数据。
```python
from faker import Faker
fake = Faker('zh_CN')
name = fake.name()
print(name)
from pyDesensitize import Desensitize
phone = '13812345678'
desensitized_phone = Desensitize().desensitize(phone, Desensitize.MOBILE)
print(desensitized_phone)
email = 'test@example.com'
desensitized_email = Desensitize().desensitize(email, Desensitize.EMAIL)
print(desensitized_email)
```
输出结果为:
```
张明
138****5678
t***@example.com
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)