截断方法:对数值、字符型,截断不需要的,保留关键数据位(156893...—留156)
import time # 做性能测试
import pandas as pd
import random
import xlwt # 先安装pip install
#随机替换函数
def replace_str_1(str, start, end):
if len(str) >= (start + end):
return str[:start] + ''.join(random.sample('0123456789', end)) + str[start+end:]
else:
return str
def replace_str(str, start, end):
if len(str) > (start + end):
return str[:start] + ''.join(random.sample('******************', end)) + str[start + end:]
else:
return str
#打开需要加密文件
data_test = pd.read_excel('....data/date_test.xlsx')
print(data_test)
#在原基础上不增新列,将total_bill改为object类型
data_test['card_t'] = data_test['card_t'].astype((str))
#需要改变的列
card_t_list = data_test['card_t']
#print(len(card_t_list))
#加密开始时间
time_start_enc = time.time()
#遍历替换
for i in range(len(card_t_list)):
card = card_t_list[i]
card = replace_str(card, 4, 10) #这是核心的那一点,调用替换
card = replace_str_1(card, 16,2)
card_t_list[i] = card
time_end_enc = time.time()
print("加密耗时s:",time_end_enc-time_start_enc)
print(data_test)
# 导出为xls文件
data_test.to_excel('....data/date_test_success.xlsx', index=False)
'''
加密耗时s: 0.008274316787719727
'''
原文件:
脱敏后: