首页 > 解决方案 > 在检查用户输入时,需要 Python 删除外部文件中逗号后的所有内容

问题描述

所以,基本上,我需要我的 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 行之后,我需要代码来检查外部文件中的用户名,逗号后没有任何内容,如果用户名已经存在,则要求用户再次输入

标签: pythonpython-3.xcoding-style

解决方案


列表.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

推荐阅读