简体   繁体   English

如何检查两个字符串是否在文件的同一行上?

[英]How do i check if two strings are on the same line in a file?

I have a program where I ask the user their username and password, and it checks a file to see if they have the correct username and password. 我有一个程序,询问用户其用户名和密码,并检查文件以查看他们是否具有正确的用户名和密码。 Each username and password is stored on its own line. 每个用户名和密码都存储在自己的行中。

this is what I have 这就是我所拥有的

if username and password in open("logininfo").read():
    print("logged in succesfully")
else:
    print("Incorrect username or password")

the problem i'm having with this is that any username can be used with any password since it check the whole file. 我遇到的问题是任何用户名都可以与任何密码一起使用,因为它可以检查整个文件。 Is there any way to check if they're on the same line in the file? 有什么方法可以检查它们是否在文件的同一行上?

You can easily make this function checking the file line by line 您可以轻松地使此功能逐行检查文件

def check_password(username, password, lines):
     for line in lines:
         if username in line and password in line:
             return True
     return False

And you can use this function this way: 您可以通过以下方式使用此功能:

check_password(username, password, open(file_name).readlines())

Iterate over open('logininfo').readlines() and check whether the username is in a line AND the password is in a line. 遍历open('logininfo').readlines()并检查用户名是否在一行中,密码是否在一行中。

In if username and password in open("logininfo") , it checks whether string username is not empty or None , which is not intended, so you need to check both the username and the password separately like this: if username and password in open("logininfo") ,它检查字符串username是否不为空或None (这不是故意的),因此您需要像这样分别检查用户名和密码:

if (username in line) and (password in line):
    ...

Resources: 资源:

Note that your code does not do what you think it does. 请注意,您的代码没有执行您认为的操作。 It doesn't check the username at all it only checks if the username is not "False". 它根本不检查用户名,仅检查用户名是否不是“ False”。

This does what you want: 这就是您想要的:

def checkpw():
    for line in open("foobar"):
        if line == (username + " " + password):
            print("logged in succesfully")
            return
    print("Incorrect username or password")

but I strongly recommend that you use some sort of library for a task like this. 但我强烈建议您对此类任务使用某种类型的库。 You should probably at least be hashing your passwords. 您可能至少应该对密码进行哈希处理。 This code is a terrible idea. 此代码是一个糟糕的主意。

You can check it reading line by line. 您可以逐行检查它。 Don't forget to close the file or open it in a with statement to free resources when you finish. 完成操作后,不要忘记关闭文件或在with语句中打开它以释放资源。

def check_user(user, password):
    with open('loginfo', 'r') as file:
        for line in file:
            if user in line and password in line:
                return True

    return False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从.txt文件读取时如何检查两个字符串是否在同一行上 - How to check if two strings are on the same line when reading from a .txt file 如何逐行读取文本文件并从文件内容中返回两个字符串和一个列表? - How do I read a text file line by line and return two strings and a list from the contents of the file? 如何检查两个字符串是否相同 - How to check if two strings are the same 在pandas数据框中,如何检查同一行但不同列中是否存在两个字符串? - In a pandas dataframe, how do I check if two strings exist on same row but in different columns? 如何检查两个html字符串是否与python等效? - How do I check if two html-strings are equivalent with python? 如何在两个不同的列表中检查相同的单词? - How do I check for the same words in two different lists? 如何在python的for循环中同时检查两个条件? - How do I check two conditions at the same time in a for loop in python? 如何检查两片 numpy 数组是否相同(或重叠)? - how do I check that two slices of numpy arrays are the same (or overlapping)? 如何检查两个变量是否在Python中引用同一个对象? - How do I check if two variables reference the same object in Python? 如何在第三个函数的同一行上调用两个函数? - How do I call two functions on the same line in third function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM