我有一个密码验证程序,并将2条不同的错误消息导出到.txt文件,然后将每一行转换为一个数组。
.txt文件的外观如下-
2019-03-30 13:29:12.490929, Password < 6
2019-03-30 13:29:18.044002, Password > 14
2019-03-30 13:42:38.230401, Password < 6
2019-03-30 13:42:40.741990, Password < 6
每次出现“ Password <6”和“ Password> 14”时,我将如何加起来并将它们分配给两个不同的变量? 希望我解释得足够好,感谢您的帮助。
答案 0 :(得分:0)
假定包含信息的文本文件称为test.txt
# extract all lines as a list
lines = [line.rstrip('\n') for line in open('test.txt', 'r')]
# set two variable counters
counter_6 = 0
counter_14 = 0
for line in lines:
# split the lines into the needed parts
# and access only the second, password part
password_part = line.split(", ")[1]
# increment for Password < 6
if "< 6" in password_part:
counter_6 += 1
# increment for Password > 14
elif "> 14" in password_part:
counter_14 += 1
print(counter_6, counter_14)
希望这会有所帮助。