i am trying to automate serial numbers issuing via .format in python, it works but it suddenly stops and restart at 105

Viewed 22

i am trying to build a function that issue a serial number then save it in JSON file per name and this serial number depends on the date as it checks the date, if its new it starts with one and save it in a JSON file, and in an excel sheet to check the JSON file and increment its value for the next name, the problem and the core of the function is the first three digits of the number as its supposed to start with 000 and increase by every name, it does that fine until it reaches 105 then it restarts to 000 here is the part of the function i suspect its wrong:

    if strdate=="01" or (month[1]!=lastserial[8]):
        i=1
        num="{:03d}".format(i)
        last=f"JO/{year}{month}{num}"
        data=f"{last}"

and here is the full function:

def genrateserial(a,trainer):
    jsonFile1 = open("data_file.json", "r")
    lastserial = jsonFile1.read()
    jsonFile1.close()
    if strdate=="01" or (month[1]!=lastserial[8]):
        i=1
        num="{:03d}".format(i)
        last=f"JO/{year}{month}{num}"
        data=f"{last}"
        jsonstring=json.dumps(data)
        jsonfile2=open("data_file.json", "w")
        jsonfile2.write(jsonstring)
        jsonfile2.close()
    i=int(lastserial[-3:-1])

    database = pd.read_csv(a,engine = 'python')
    df = pd.DataFrame(database)
    df = df.dropna(axis=0)
    for z in range(len(df.Name)):
        i=i+1
        num= "{:03d}".format(i)
        newentry=f"JO/{year}{month}{num}"

        jsonstring1=json.dumps(newentry)
        jsonfile3=open("data_file.json","w")
        jsonfile3.write(jsonstring1)
        jsonfile3.close()
        df.loc[z,'serial number']=newentry
        df.loc[z, 'trainer'] = trainer
    df.to_csv(a, index=False)
0 Answers
Related