Increment a numeric string in 1

Asked

Viewed 384 times

2

I want to add 1 to a counter that is represented as a string. the goal is to get the sequence of numbers later. For example: from 000570370 to 000570371.

part of the code:

 def rangeOrdemServ(self):
        db = self.conexaoBanco()
        cursor = db.cursor()
        cursor.execute("SELECT ordem_serv_num FROM ordem_serv ORDER BY id DESC LIMIT 1")
        dadosOrdemServ = cursor.fetchone()
        db.commit()
        global rec
        dados1 = [dadosOrdemServ]
        dados2 = [1]
        dados3 = (dados1) + (dados2)

        self.entrOrdServ.insert(END,dados3)

when I run this code get the following answer: [('000005703701021',), 1], and not the expected 000005703701022

  • Why are the clasps?

  • without brackets of the following error: 'data3 = data1 + data2 Typeerror: can only concatenate tuple (not "int") to tuple'

  • dadosOrdemServ is a String. Do you want to treat it as a number to add up 1? Is that it? Because, if it is, it has nothing to do with concatenation.

  • Yes it is, I work a short time with python, I do not have much experience, can help me with this question?

1 answer

2


Use this function:

def incrementarStringNumericaEmUm(stringNumerica):

    qtdDigitosComZeros = len(stringNumerica)
    originalComoNumero = int(stringNumerica)
    qtdDigitosSemZeros = len(str(originalComoNumero))
    resultadoComoNumero = originalComoNumero + 1

    qtdZerosNaOriginal = qtdDigitosComZeros - qtdDigitosSemZeros


    if len(str(resultadoComoNumero)) == qtdDigitosSemZeros:
        # Não houve "vai um"
        return ('0' * qtdZerosNaOriginal) + str(resultadoComoNumero)
    else:
        # Aumentou um dígito no resultado, então precisamos colocar um zero a menos
        return ('0' * (qtdZerosNaOriginal - 1)) + str(resultadoComoNumero)

It takes a numeric string in the format you have in dadosOrdemServ and returns a numeric string with the value incremented in one. For this she counts how many zeros she has in the original, then converts the original to an integer, increments that number into one, turns that number into a string and concatenates back the zeros, taking care to put a minus zero in case of "will a".

  • gave this error: originalComumero = int(data) Typeerror: int() argument must be a string or a number, not 'tuple'

  • It’s not to pass dados, but yes dadosOrdemServ.

  • made the same mistake

  • Take a printOrdemServ and tell me what comes out.

  • As far as I can see, dataOrdemServ is a tuple. pass dadosOrdemServ[0] for the function, then.

  • when I give a print out: ('000005703701021',)

  • Pass dadosOrdemServ[0] for the function, then.

  • gave no error now but gave no print either, where I recover the answer

  • The answer is the function return. Type: numeroIncrementado = incrementarStringNumericaEmUm(dadosOrdemServ[0])

  • in my case the answer I need to be inserted within an entry, ex: self.entrOrdServ.Insert(END,reply)

  • Use the value returned by the function to do the insertion. If you are having problems inserting something into the bank, this would be the subject of another question.

  • it worked I used the resultComumero to insert the answer and it gives the sequence straight, obrg by force

  • Quiet. Good job.

Show 8 more comments

Browser other questions tagged

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