Hello,
I looked at several sites and did not find it, so I decided to ask here. I have a relational database table, MySQL, which has a single column for both CPF and CNPJ. In addition, this column is in the bigint format, that is, the leading zeros are suppressed. I just need to get the CPFs. Would it be enough to use the CPF validator right? However, I noticed that there are records that have less than 11 digits and pass in the validation of both CPF and CNPJ. So, does anyone know any other validation that allows me to differentiate a CPF from a CNPJ? Here are the codes I'm using to validate CPF and CNPJ and follow sample records that pass both validations: 980258480, 343522950 and 937439126.
def isCPFValido(cpf, d1=0, d2=0, i=0):
if cpf is not None:
if len(cpf) > 11:
return False
while i < 10:
d1, d2, i = (d1 + (int(cpf[i]) * (11 - i - 1))) % 11 if i < 9 else d1, (
d2 + (int(cpf[i]) * (11 - i))) % 11, i + 1
return (int(cpf[9]) == (11 - d1 if d1 > 1 else 0)) and (int(cpf[10]) == (11 - d2 if d2 > 1 else 0))
else:
return False
def validar_cnpj(cnpj):
if not isinstance(cnpj, basestring):
cnpj = str(cnpj)
cnpj = format_cnpj(cnpj)
cnpj = ''.join(re.findall('\d', cnpj))
if (not cnpj) or (len(cnpj) < 14):
return False
# Pega apenas os 12 primeiros dígitos do CNPJ e gera os 2 dígitos que faltam
inteiros = map(int, cnpj)
novo = inteiros[:12]
prod = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
while len(novo) < 14:
r = sum([x*y for (x, y) in zip(novo, prod)]) % 11
if r > 1:
f = 11 - r
else:
f = 0
novo.append(f)
prod.insert(0, 6)
# Se o número gerado coincidir com o número original, é válido
if novo == inteiros:
return cnpj
return False