So I have a csv file that contains full credit-card numbers.. We dont need the full number, and so I am writing a quick script to parse through the csv and replace the cc number with a masked representation. (all *'s except the last four). I am pretty new to python and hacked this up, and it works, but in order to learn I want to know if it could be done easier.
Assume that "str" will be a full creditcard number. But for the sake of my example I am just using the string "CREDITCARDNUMBER".
str = "CREDITCARDNUMBER";
strlength = len(str)
masked = strlength - 4
slimstr = str[masked:]
print "*" * masked + slimstr
The output is exactly what I want
************MBER
But I am sure there is a more elegant solution. :) Thanks!
Use 4 issuer digits. The first four digits of the credit card number are copied from the source to the output. The remaining part of the credit card number is appended with the masked account number and a check digit. A check digit is a digit added to a number that validates the authenticity of the number.
In Python, we use double underscore before the attributes name to make them inaccessible/private or to hide them.
For those who want to keep Issuer Identification Number (IIN) (previously called the "Bank Identification Number" (BIN)), which is usually the first 6 digits, that should do the job:
print card[:6] + 'X' * 6 + card[-4:]
'455694XXXXXX6539'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With