我有一个包含5列和大约5000行的.csv文件。在.csv文件中名为“汇总”的特定列中,有信用卡号和一些文本。看上去像这样
嘿,这份工作需要尽快完成,并用card#签证5611000043310001支付
我想读这篇专栏文章,取出数字(可能使用正则表达式),然后屏蔽最后4位数字,然后在.csv文件中写出整个行,就像用这样的蒙面数字一样。
嘿,这份工作需要尽快完成,并支付card#签证561100004331*
我该怎么做呢?
发布于 2019-07-12 02:53:00
使用regex,您可以:
import re
>> s = "hey this job needs to be done asap and pay with card# visa 5611000043310001"
>> re.sub(r"(\d{12})\d{4}",r"\1****",s)
'hey this job needs to be done asap and pay with card# visa 561100004331****'因此,基本上,(\d{12})匹配前12位数字(括号是用来代替前12位的)。然后是四位数,我们用星星来代替。\1是替换所省略的第一组的占位符,因此这里引用前12位数字。
发布于 2019-07-12 03:11:57
下面使用regex的替换函数查找确切的16位数字,并隐藏最后4位数字。
所以这个代码:
eg_summaries = [
'blah blah card# visa 5611000043310001',
'blah blah card# visa 5611000043310001 with text after',
'5611000043310001',
'visa: 5611000043310001 and random number > 16 digits: 0011237324763246723487243',
]
df = pd.DataFrame({'summary': eg_summaries })
df['summary'].replace(r'\b(\d{12})\d{4}\b', r'\1****', inplace=True, regex=True)
print (df.summary)应该打印出来:0 blah blah card# visa 561100004331**** 1 blah blah card# visa 561100004331**** with text after 2 561100004331**** 3 visa: 561100004331**** and random number > 16 digits: 0011237324763246723487243
https://stackoverflow.com/questions/56999525
复制相似问题