简体   繁体   English

有效计数器计算来自python文本文件的错误输入尝试

[英]efficient counter to count incorrect input attempt from text file in python

I am making a program that reads a text file that has logged values of incorrect input attempts (whether it's 'password < 6' or 'password > 10' characters).我正在制作一个读取文本文件的程序,该文件记录了错误输入尝试的值(无论是“密码 < 6”还是“密码 > 10”字符)。 The program prints the list horizontally and makes it quite long so I wanted to make it vertical for easier read but I don't know how to.该程序水平打印列表并使其很长,因此我想使其垂直以便于阅读,但我不知道如何操作。 Also, for every log (password < 6 or password > 10), I was hoping to make a counter which states how many times the statements have occurred in the log file and again I don't really know how to do it.此外,对于每个日志(密码 < 6 或密码 > 10),我希望制作一个计数器,说明日志文件中出现了多少次语句,但我真的不知道该怎么做。

Here's the code:这是代码:

def main():
    with open("ITWorks_password_log.txt", "r") as ITWorks_password_log:
        lines = ITWorks_password_log.readlines()

    time = []
    pass_characters = []

    for l in lines:
        as_list = l.split(", ")
        time.append(as_list[0])
        pass_characters.append(as_list[1].replace("\n", ""))

    count_pw_too_small = "password < 6"
    count_pw_too_large = "password > 10"

    print(time)
    print(pass_characters)


main()

1: Sample of Text File 1:文本文件示例

2021-07-22 16:24:42.843103, password < 6
2021-07-22 16:24:44.963020, password < 6
2021-07-22 16:24:49.327202, password > 10
2021-07-22 16:24:52.810838, password > 10
2021-07-22 16:24:57.057562, password > 10
2021-07-22 16:24:58.961836, password < 6
  1. Envisioned Output Two brackets with time and incorrect attempt label total count for attempts lower than 6 characters total count for attempts higher than 10 characters预想的输出 两个带有时间和错误尝试标签的括号 小于 6 个字符的尝试总数 大于 10 个字符的尝试总数

The answer is pretty much, "just do it" for a lot of these questions, but since you did post a good bit of code, I'll suggest an alternative for your data structure.对于很多这样的问题,答案几乎是“就去做”,但是由于您确实发布了大量代码,因此我将为您的数据结构建议一个替代方案。 Store all the reasons in a dict, as you parse each line.在解析每一行时,将所有原因存储在 dict 中。 I used defaultdict merely for convenience.我使用 defaultdict 只是为了方便。

from collections import defaultdict

def main():
    with open("sample.txt", "r") as f:
        results = defaultdict(list)
        for line in f.readlines():
            timestamp, reason = line.strip().split(", ")
            # you should probably read the timestamp into an actual datetime object here
            results[reason].append(timestamp)
        
        for reason, timestamps in results.items():
            print ('The reason "{}" occurred {} times.'.format(reason, len(timestamps)))

main()

The reason "password < 6" occurred 3 times.原因“password < 6”出现了 3 次。
The reason "password > 10" occurred 3 times.原因“password > 10”出现了 3 次。

Of course, if you only care about the counts, you do not need the individual timestamps.当然,如果您只关心计数,则不需要单独的时间戳。 You can just store the count in a dict or defaultdict(int).您可以将计数存储在 dict 或 defaultdict(int) 中。

If you do want to keep all the data in a collection, then you can pass that collection to collections.Counter to get a similar result, with the downside that you'd iterate over the whole list a second time-uh.如果您确实希望将所有数据保留在一个集合中,那么您可以将该集合传递给 collections.Counter 以获得类似的结果,但缺点是您将再次遍历整个列表 - 呃。 But that's not such a big deal.但这没什么大不了的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM