在检查用户输入时需要 Python 删除外部文件中逗号后的所有内容

Need Python to Remove everything after a comma in external file while checking user input

所以,基本上,我需要 python 代码从外部文件读取每一行并删除逗号后的所有内容。然后,它应该使用新字符串检查用户输入。

就像一个验证过程,用户注册然后代码检查用户名是否被占用

但在外部文件中,用户详细信息存储为: 'username,password'

这是我的代码:

    LogFile = 'accounts.txt'
    invalidUserName = False
    Uusername = input("Please enter your desired username: ")
    while len(Uusername) < 3 or len(Uusername) > 16:
        Uusername = input("Username is not between the length of 3 and 16, please re-enter: ")

    with open(LogFile) as f:
        content = f.readlines()
        contnt = [l.strip() for l in content if l.strip()]
        passlist = []
        userlist = []
        for line in content:
            try:
                userlist.append(line.split(",")[0])
                passlist.append(line.split(",")[1])
            except IndexError:
                pass
        for username in userlist:
            while username == Uusername:
                Uusername = input(('Username is taken, please enter another one: '))
                invalidUserName = True

    if not invalidUserName:
        password = input("Please enter your desired password: ")
        while len(password) < 3 or len(password) > 16:
            password = input("Password is not between the length of 3 and 16, please re-enter: ")
        while password == username:
            password = input("Password cannot be the same as the username, please re-enter: ")
        VerPW = input("Please verify your password: ")
        while VerPW != password:
            VerPW = input("Passwords do not match, try again: ")
        file = open ('accounts.txt','a')
        file.write (username +','+password + "\n")
        file.flush()
        file.close
        print ("Details stored successfully")

在第 5 行之后,我需要代码来检查外部文件中的用户名,逗号后没有任何内容,如果用户名已经存在则要求用户再次输入

list.txt:

emkay,123
alibaba,420

khanbaba,5647
naughty_princess,3242
whatthehack,657

然后:

logFile = "list.txt"

with open(logFile) as f:
    content = f.readlines()

# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]


# list to store the passwords
passList = []

# list to store the usernames
userList = []
for line in content:
    userList.append(line.split(",")[0])
    passList.append(line.split(",")[1])


print(userList)
print(passList)

user_inp = input("Enter a username: ")

for username in userList:
    if username == user_inp:
        print("Username: {}, already taken. Please try a different username".format(username))

输出:

['emkay', 'alibaba', 'khanbaba', 'naughty_princess', 'whatthehack']
['123', '420', '5647', '3242', '657']
Enter a username: naughty_princess
Username: naughty_princess, already taken. Please try a different username

编辑:

如果您希望循环在找到用户名时停止。简单地说,break离开。

for username in userList:
    if username == user_inp:
        print("Username: {}, already taken. Please try a different username".format(username))
        break

编辑 2:

在您刚刚发布的代码中应用它,

if username == Uusername:
       Uusername = ('Username is taken, please enter another one: ')
       break     # or use continue as it looks like you need it

编辑 3:

您的代码已修复,您需要为无效用户设置一个布尔标志,然后按以下步骤进行操作:

LogFile = 'accounts.txt'
invalidUserName = False
Uusername = input("Please enter your desired username: ")
while len(Uusername) < 3 or len(Uusername) > 16:
    Uusername = input("Username is not between the length of 3 and 16, please re-enter: ")

with open(LogFile) as f:
    content = f.readlines()
    contnt = [l.strip() for l in content if l.strip()]
    passlist = []
    userlist = []
    for line in content:
        try:
            userlist.append(line.split(",")[0])
            passlist.append(line.split(",")[1])
        except IndexError:
            pass
    print(userlist)
    for username in userlist:
        if username == Uusername:
            print('Username is taken, please enter another one:')
            invalidUserName = True
            break
if not invalidUserName:
    password = input("Please enter your desired password: ")
    while len(password) < 3 or len(password) > 16:
        password = input("Password is not between the length of 3 and 16, please re-enter: ")
    while password == username:
        password = input("Password cannot be the same as the username, please re-enter: ")
    VerPW = input("Please verify your password: ")
    while VerPW != password:
        VerPW = input("Passwords do not match, try again: ")
    file = open ('accounts.txt','a')
    file.write (username +','+password + "\n")
    file.flush()
    file.close
    print ("Details stored successfully")

编辑 4:

LogFile = 'accounts.txt'
invalidUserName = False
while True:
    Uusername = input("Please enter your desired username: ")
    while len(Uusername) < 3 or len(Uusername) > 16:
        Uusername = input("Username is not between the length of 3 and 16, please re-enter: ")

    with open(LogFile) as f:
        content = f.readlines()
        contnt = [l.strip() for l in content if l.strip()]
        passlist = []
        userlist = []
        for line in content:
            try:
                userlist.append(line.split(",")[0])
                passlist.append(line.split(",")[1])
            except IndexError:
                pass
        print(userlist)
        for username in userlist:
            if username == Uusername:
                print('Username is taken, please enter another one:')
                invalidUserName = True
                break
    if not invalidUserName:
        password = input("Please enter your desired password: ")
        while len(password) < 3 or len(password) > 16:
            password = input("Password is not between the length of 3 and 16, please re-enter: ")
        while password == username:
            password = input("Password cannot be the same as the username, please re-enter: ")
        VerPW = input("Please verify your password: ")
        while VerPW != password:
            VerPW = input("Passwords do not match, try again: ")
        file = open ('accounts.txt','a')
        file.write (username +','+password + "\n")
        file.flush()
        file.close
        print ("Details stored successfully")

        exit_now = input("Press 1 to exit")

        if '1' in exit_now:
            break