Differentiate CPF and CNPJ that are in the same Mysql database column

Asked

Viewed 1,140 times

2

Hello,

I looked at several websites and couldn’t find them, so I decided to ask here. I have a relational database table, Mysql, which has a single column for both CPF and CNPJ. Also, this column is in bigint format, that is, the zeroes on the left are suppressed. I need to get only the Cpfs. Just use the right number validator? However, I noticed that there are records that have less than 11 digits and pass the validation of both CPF and CNPJ. So, does anyone know any other validation that would allow me to differentiate in fact, a CPF from a CNPJ? Follow the codes I’m using to validate CPF and CNPJ and follow examples of 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
  • if Voce selected the values in the column that are > 11 dgtos, these are for sure CNPJ, isolate them and validate, select all those that are <= 11, isolate them and validate, the ones that are left, Oce needs to check why for a request

  • (F* Enter, grrr...)quiz to say, for a more precise request of the values of this column, they will necessarily have to be correct in the real world, to isolate them, Oce can select the fields as text and validate their length, being > 11, we have the Cnpjs and so on, for those who are < 11, you will have to write even in case of their need in fact.

  • I had a case similar to this a long time ago, I had to do this to separate and by coincidence also had several numbers with length < 11, 'deo a chipped BO', rerere, when needed these numbers

1 answer

1

Hello!

You can use brazilnum

from brazilnum.cnpj import validate_cnpj
from brazilnum.cpf import validate_cpf

# Retorna True se for um CNPJ válido
validate_cnpj(documento)

# Retorna True se for um CPF válido
validate_cpf(documento)

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.