我正在尝试编写一个程序来处理客户数据,找到完整的SSN,并用正则表达式用X以及SSN的最后4位代替它。我迷路了,还没走很远。
这里是我已经拥有的。任何建议或帮助将不胜感激:
import csv
import re
data = []
with open('customerData.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
print(data[0])
print(data[1]["Name"])
print(data[2]["Spent Past 30 Days"])
social = re.search(\d{3}-\d{2}-\d{4})
SyntaxError:行继续符后出现意外字符。
这是我的第一篇文章,我对Python还是很陌生,因此我对格式不正确表示歉意。
以下是一个示例,它替换了以下形式的社交形式:123-45-6789(即组之间的破折号)
# Pattern to find social security numbers (use capture group on last 4 digits)
p = re.compile(r"\d{3}-\d{2}-(\d{3})")
# Example string with multiple numbers
s = "first social security number 012-23-3423 here's a second number 201-55-2155 and so on"
# substitutes for socials in string
result = p.sub(r'XXX-XX-\1', s) # keeping digits of capture group (i.e. \1)
print(result)
结果
输入
"first social security number 012-23-3423 here's a second number 201-55-2155 and so on"
输出
"first social security number XXX-XX-3423 here's a second number XXX-XX-2155 and so on"