سؤال

"expo","81111111","05/Apr/2014:02:24:02 +0200","GET / HTTP/1.1","200","simpleTV/0.4.7","-","0" "expo","11.11.11.11.","05/Apr/2014:02:24:04 +0200","GET / HTTP/1.1","200","simpleTV/0.4.7","-","0" "expo","11.11.11.11","05/Apr/2014:02:24:05 +0200","GET / HTTP/1.1","200","simpleTV/0.4.7","-","0" "mahmut","811.11.11.11","05/Apr/2014:02:24:05 +0200","GET / HTTP/1.1","200","simpleTV/0.4.7","-","0" "kerem","13.12.13.12","05/Apr/2014:02:24:06 +0200","GET / HTTP/1.1","200","simpleTV/0.4.7","-","0" "cengiz","12.12.12.12","05/Apr/2014:02:24:07 +0200","GET / HTTP/1.1","200","simpleTV/0.4.7","-","0"

this is my log file and i would like to read each information if ip address is same in two lines or more line i would like to python only display one line and tells me the how many times that ip address shows up in this log. also after that i would like to create a new txt file. thanks for the help!

هل كانت مفيدة؟

المحلول

You could start reading the logfile using a csv.DictReader:

header = ["site", "ip", "date", "method","code", "browser","n1", "n2"]

# dict to store [ip] -> occurences
# this defaultdict returns 0 for unset keys
ip_freq = defaultdict(0)

with open("filename.csv", mode="r") as csvfile:
    reader = csv.DictReader(csvfile, header, delimiter=",", quoting=csv.QUOTE_ALL);
    for line in reader:
        # sets the ip and increments the number of occurences
        ip_freq[line["ip"]] += 1
        # print: "requestcount: 2   ip: 85.97.166.82"
        print("requestcount: {}\tip: {}".format(
                                        ip_freq[line["ip"]], 
                                        line["ip"]))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top