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

标签 python for-loop text-files

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

使用当前的解决方案,我可以获得要打印的密码列表,但是当检查密码是否强时,只显示一个结果。我试图将输出作为结果生成的密码,例如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")
            

我收到的输出如下

Output received

因此,文本文件中的 5 个密码列表中似乎只检查了一个密码。我相信可能需要在某个地方有一个 for 循环来检查每个密码,但我不确定如何解决这个问题。我想到的另一种方法是将文本文件中的密码插入到列表中,并迭代该列表以获取每个密码的结果。这听起来像是解决这个问题的正确方法吗?

任何有关此问题的帮助将不胜感激。

谢谢

最佳答案

我已经写了一个解决方案。我为代码添加了许多注释作为描述。

代码:

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

关于Python-读取文本文件的每一行并将每一行传递给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62772692/

相关文章:

python - 将函数 "inside"设为模块

java - 永远运行而不停的for循环出错

java - 如果一行包含 JavaFX 中的特定文本,如何突出显示该行

arrays - 如何在结构数组中映射文本文件内容?

python - Django Python如何计算给定两个时间字符串的时间差

python - 将列表从一个 Dataframe 行映射到另一个 Dataframe 行的矢量化方法

python - Python 中的饼图和条形图

java - For 循环不等待 JTextField 中的用户响应

c++ - cin 不会在循环中工作

javascript文本压缩/解压缩