【问题标题】:Read only lines and not the entire thing in python 2.7只读行而不是 python 2.7 中的全部内容
【发布时间】:2018-02-10 16:33:39
【问题描述】:

如何在下面的代码中注册用户名和密码时,它会在 txt 文件中添加一个新行,当你登录时,它会单独检查 txt 文件中的每一行 如果任何一行与登录名匹配,则声明它是正确的,而不是检查用户名和密码是否与整个内容匹配

print "Welcome to UserName and Password Test"
option = raw_input("Would you like to login or register L for Login R for 
register: ")
if option == "R":
    print "Warning Only 1 user can be registered"
    usernamer = raw_input("Desired Username: ")
    passwordr = raw_input("Desired Password: ")
    file = open("Username.txt","w")
    file.write(usernamer + '\n')
    file.close()
    file = open("Password.txt","w")
    file.write(passwordr + '\n')
    file.close
    print "Registered"
else:
    usr = raw_input("Username: ")
    pss = raw_input("Password: ")
    file = open("Username.txt","r")
    if usr == file.read():
        print "Username correct"
        file.close()
    else:
            print "Username incorrect or not registered"
            file.close()
    file = open("Password.txt","r")
    if pss == file.read():
        print "Password correct"
        file.close()
    else:
            print "Password incorrect or not registered"
            file.close()

【问题讨论】:

  • 以附加模式打开(使用'a'而不是'w')将添加到文件的末尾。使用file.readlines() 获取文件中的行列表,然后使用循环检查每一行是否匹配。

标签: python python-2.7


【解决方案1】:

以下是如何查找文件的某行中是否存在字符串:

contain = False # Indicator of whether some line of a file contains the string
with open(FILENAME, 'r') as file: # Use `with` statement to automatically close file object
    for line in file:
        if string in line:
            contain = True
            break

【讨论】:

    【解决方案2】:

    我已修改您的代码以使用密码检查逻辑验证密码和用户名。这应该可以。

    print "Welcome to UserName and Password Test"
    option = raw_input("Would you like to login or register L for Login R for register: ")
    if option == "R":
        print "Warning Only 1 user can be registered"
        usernamer = raw_input("Desired Username: ")
        passwordr = raw_input("Desired Password: ")
        file = open("Username.txt","a")
        file.write(usernamer + '\n')
        file.close()
        file = open("Password.txt","a")
        file.write(passwordr + '\n')
        file.close
        print "Registered"
    else:
        usr = raw_input("Username: ")
        pss = raw_input("Password: ")
        file = open("Username.txt","r")
        count = 0
        userfound = False
        try:
            for user in file.readlines():
                count += 1
                if usr == user.strip():
                    userfound = True
            file.close()
            if not userfound:
                raise
            file = open("Password.txt","r")
            passcount = 0
            for passcode in file.readlines():
                passcount += 1
                if count == passcount:
                    if pss == passcode.strip():
                        print 'successfully logged in'
                    else:
                        print "Password incorrect or not registered"
                    break
    
            file.close()
        except Exception as e:
            print 'User login error !!!'
    

    【讨论】:

      【解决方案3】:

      如果你只是想检查用户是否在文件内容中,

      如果 usr 在 file.read():
      做某事

      "==" 检查值是否与其相同,"in" 关键字检查子字符串是否在大字符串中。在 file.read() 中会将整个内容作为一个字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-03
        • 2015-01-18
        • 1970-01-01
        • 1970-01-01
        • 2014-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多