Python - 读取文本文件的每一行并将每一行传递给变量

Python - Read each line of text file and pass each line to variable

我正在尝试实现一个简单的密码检查器,它允许用户打开一个包含生成的密码的文本文件,并根据标准列表检查它们以确定密码是强密码还是弱密码。然后它应该输出密码,结果是强还是弱。

使用当前的解决方案,我可以获得要打印的密码列表,但是当检查密码是否强时,只显示一个结果。我正在尝试将输出作为结果生成的密码,例如Randompassword123 - 这是一个弱密码。

下面是我目前使用的代码:

def multiplestrength(): # password strength check function 
        textfile =  filedialog.askopenfilename(initialdir="/home", title = "Select text file to split",filetypes = (("text files","*.txt"),("all files","*.*")))
        
     
        with open(textfile , mode="r",encoding="utf-8") as my_file:
            data=my_file.read()
            print(data)
        
        def strongPassword(data):
            
                if passRegex1.search(data) == None:
                    return False
                if passRegex2.search(data) == None:
                    return False
                if passRegex3.search(data) == None:
                    return False
                if passRegex4.search(data) == None:
                    return False
                else:
                    return True

        passRegex1 = re.compile(r'\w{8,}')
        passRegex2 = re.compile(r'\d+')
        passRegex3 = re.compile(r'[a-z]')
        passRegex4 = re.compile(r'[A-Z]')

        


        if strongPassword(data) == True:
            print("Strong Password")
        else:
            print("This is not a strong password")
            

我收到的输出如下

因此,文本文件中的 5 个密码列表中似乎只检查了一个密码。我相信可能需要在某个地方有一个 for 循环来检查每个密码,但我不确定用什么方法来解决这个问题。我考虑的另一种方法是将文本文件中的密码插入到列表中,然后遍历该列表以获得每个密码的结果。这听起来像是解决这个问题的正确方法吗?

如有任何帮助,我们将不胜感激。

谢谢

您可以使用以下方式遍历文件的行:

with open(textfile , mode="r",encoding="utf-8") as my_file:
  for line in my_file:
    # do something with the line, eg:
    if strongPassword(line):
       # ...

编辑:您可能想使用 line.strip() 而不是 line 来去掉末尾的换行符 (\n)

我已经写了一个解决方案。我已经为代码添加了很多评论作为描述。

代码:

import re

passRegex1 = re.compile(r'\w{8,}')
passRegex2 = re.compile(r'\d+')
passRegex3 = re.compile(r'[a-z]')
passRegex4 = re.compile(r'[A-Z]')


def strong_password(data):  # the function name should be snake case
    if not passRegex1.search(data):  # Use "if not". It is the Pythonic way. It handles False/None/0/""/[] etc...
        return False
    if not passRegex2.search(data):
        return False
    if not passRegex3.search(data):
        return False
    if not passRegex4.search(data):
        return False
    else:
        return True


with open("test.txt", mode="r", encoding="utf-8") as my_file:  # Open file for reading
    for line in my_file.readlines():  # Read all lines one-by-one
        print("\nPassword: {}".format(line.strip()))  # Print the current password ("strip" removes the whitespace characters from string).
        if strong_password(line):  # This statement is True if the "strong_password" function returns True
            print("Strong Password")  
            continue  # Get the next element (line of file)
        print("This is not a strong password")  # Else statement is not needed because the "if" contains a continue

我的测试文件:

asdf
121234
adsf123
asdffdsatre323423
fdggfd2323____,,,**
tt
333345
asdfSDFGRAdsfAERTGRghRGads___++((((FDsaSDfAS4233423524
434
55555

输出:

>>> python3 test.py

Password: asdf
This is not a strong password

Password: 121234
This is not a strong password

Password: adsf123
This is not a strong password

Password: asdffdsatre323423
This is not a strong password

Password: fdggfd2323____,,,**
This is not a strong password

Password: tt
This is not a strong password

Password: 333345
This is not a strong password

Password: asdfSDFGRAdsfAERTGRghRGads___++((((FDsaSDfAS4233423524
Strong Password

Password: 434
This is not a strong password

Password: 55555
This is not a strong password