Skip to content

Instantly share code, notes, and snippets.

@joaopcnogueira
Last active June 12, 2019 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joaopcnogueira/cc228778678cf7d3f79ef26e7b4e8722 to your computer and use it in GitHub Desktop.
Save joaopcnogueira/cc228778678cf7d3f79ef26e7b4e8722 to your computer and use it in GitHub Desktop.
CPF string to number
import re
my_string = "012.345.678-09"
digits_pattern = r"\d+"
digits = re.findall(digits_pattern, my_string)
number = int(''.join(digits))
print(number)
# output: 1234567809
###############
# another way #
###############
digits_pattern = r"[.-]"
digits = re.split(digits_pattern, my_string)
number = int(''.join(digits))
print(number)
# output: 1234567809
@joaopcnogueira
Copy link
Author

Function to decode CPF's in string format to number (int) format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment