Click here to Skip to main content
16,019,764 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's my question, For the account name above, if an account name already exists, use an account name appended with an integer to distinguish different account names. For example, if there are three patients with the same account name “abranch”, the second one should be named as “abranch1” and the third one should be named as “abranch2”. How to elaborate my program by using an account name appended with an integer to distinguish different account names?

Here's are my template of the name list:

I would be most appreciate if you help me!

What I have tried:

with open('all_patients.txt', 'r') as fh:
    for line in fh:

        splited_list = line.split()
        surname = splited_list[1]
        given_name = splited_list[0][0]
        patient_name_u = given_name + surname
        patient_name.append(patient_name_u.lower())

fh.close()
print(patient_name)
Posted
Updated 13-Oct-22 21:27pm

You will need to look through your list of existing account names, and find all those that have the form "account name" + "_" + a number.
A regex is probably a good way to do that:
^(AccountNameYouAreLookingFor)(\d+)$

Then convert the second group - the digits - into a number, find the highest one, and add one to create your new account name.
 
Share this answer
 
Create a 5. Data Structures — Dictionary[^] type so you can check each account for uniqueness. If the account already exists then add the number to it and add it to the dictionary. If it does not already exist then just add it as it is.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900