0
点赞
收藏
分享

微信扫一扫

python 简单的数据脱敏


最近要做一个生产数据提取,涉及到隐私数据脱敏。因提取的数据是比较规范化的数据,就想到了用python的string特性来做。

对于指定分隔符或者定长的文件,使用python来实现是比较得心应手的。很简单,几行代码完成。

#!/usr/bin/env python3

sourcefilepath = "C:/Users/mingjie1212/Desktop/intellij idea/123.txt"

# 123.txt文件
# 1|2|333333333333|aaaaaaaaaa|4444444444444|
# 1|2|333333333333|aaaaaaaaaa|4444444444444|
# 1|2|333333333333|aaaaaaaaaa|4444444444444|
# 1|2|333333333333|aaaaaaaaaa|4444444444444|
# 1|2|333333333333|aaaaaaaaaa|4444444444444|
# 1|2|333333333333|aaaaaaaaaa|4444444444444|
# 1|2|333333333333|aaaaaaaaaa|4444444444444|

replace_key = '******'

#首先读取文件
with open(sourcefilepath, 'r', encoding='utf-8') as f:
contents = f.readlines()

# 特殊字段分割 脱敏指定列字符,如脱敏第4列
column_num=4
for line in contents:
temp = line.split("|")
org_str = temp[column_num-1]
replace_str = org_str[0:3] + replace_key + org_str[5:]

target_line = ""
for i in range(len(temp)):
if i == column_num-1:
target_line = target_line + "|" + replace_str
elif i == len(temp) - 1:
target_line = target_line + temp[i]
else:
target_line = target_line + temp[i] + "|"

print(target_line)

# 定长文件 脱敏指定列字符
for line in contents:
replace_str = line[0:3] + replace_key + line[5:]
print(replace_str)

参考:​​https://www.w3schools.com/python/python_strings.asp​​

举报

相关推荐

0 条评论