3

I've spent the last two days trying to figure out how to increment a numeric string in Python. I am trying to increment a sequence number when a record is created. I spent all day yesterday trying to do this as an Integer, and it works fine, but I could never get database to store leading zeros. I did extensive research on this topic in StackOverflow, and while there are several examples of how to do this as an Integer and store leading zeros, none of the examples worked for me. Many of the examples were from 2014, so perhaps the methodology has changed. I then switched over to a String and changed my attribute to a CharField, and can get the function to work with leading zeros, but now I can't seem to get it to increment. Again, the examples that I found on SO were from 2014, so maybe things have changed a bit. Here is the function that works, but every time I call it, it doesn't increment. It just returns 00000001. I'm sure it's something simple I'm not doing, but I'm out of ideas. Thanks in advance for your help. Here is the function that works but doesn't increment.

def getNextSeqNo(self):
    x = str(int(self.request_number) + 1)
    self.request_number = str(x).zfill(8)
    return self.request_number

Here is the field as it's defined:

request_number = models.CharField(editable=True,null=True,max_length=254,default="00000")

I added a default of "00000" as the system is giving me the following error if it is not present:

int() argument must be a string, a bytes-like object or a number, not 'NoneType'

I realize the code I have is basically incrementing my default by 1, which is why I'm always getting 00000001 as my sequence number. Can't seem to figure out how to get the current number and then increment by 1. Any help is appreciated.

7
  • 2
    There is no such thing as a numeric string. If it's a string, it's not a number; it's a sequence of characters, some or all of which happen to be numeric characters. Numbers don't have leading zeros; there is no such decimal number as 01 or 001. You don't do math on strings. If you need to do math, convert it to an actual number, do the calculation, and then convert it back to a string, and left pad with zero characters until you've got the desired length. Commented Dec 21, 2017 at 16:36
  • 1
    Off-topic, but why on earth is it a CharField? Why not an Integer? Commented Dec 21, 2017 at 16:37
  • 1
    Are you sure self.request_number is set? It may not be if it's giving you the default Commented Dec 21, 2017 at 16:39
  • Thanks for the feedback. I am trying to keep a record number such as 00001, 00002, 00003. I can show the numbers fine by adding 0s at the presentation layer, but want people to be able to query the record numbers with 0000s as well. If there is a better way to approach this, I'm open to suggestions. Commented Dec 21, 2017 at 16:41
  • What benefit does the user get by being able to search on request_number=0001 instead of just request_number=1? Commented Dec 21, 2017 at 16:43

4 Answers 4

5

A times ago I made something similar

You have to convert your string to int and then you must to get its length and then you have to calculate the number of zeros that you need

code = "00000" 
code = str(int(code) + 1 )
code_length = len(code)

if code_length < 5: # number five is the max length of your code
  code = "0" * (5 - code_length) + code

print(code)
Sign up to request clarification or add additional context in comments.

Comments

3

Can this be done? Yes. But don't do it.

Make it an integer.

Incrementing is then trivial - automatic if you make this the primary key. For searching, you convert the string to an integer and search the integer - that way you don't have to worry how many leading zeros were actually included as they will all be ignored. Otherwise you will have a problem if you use 6 digits and the user can't remember and puts in 6 0's + the number and then doesn't get a match.

Comments

0

For those who want to just increase the last number in a string.

Import re

a1 = 'name 1'
num0_m = re.search(r'\d+', str(a1))
if num0_m:
    rx = r'(?<!\d){}(?!\d)'.format(num0_m.group())
    print(re.sub(rx, lambda x: str(int(x.group()) + 1), a1))

Comments

0
number = int('00000150')
('0'*7 + str(number+1))[-8:]

This takes any number, adds 1, concatenates/joins it to a string of several (at least 7 in your case) zeros, and then slices to return the last 8 characters.
IMHO simpler and more elegant than measuring length and working out how many zeros to add.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.