Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python计算文件中字符串的唯一出现次数_Python_Python 3.x - Fatal编程技术网

Python计算文件中字符串的唯一出现次数

Python计算文件中字符串的唯一出现次数,python,python-3.x,Python,Python 3.x,我试图使用python 3.3.1计算Apache日志文件中的唯一IP地址 问题是我认为它没有正确地计算每件事 这是我的密码: import argparse import os import sys from collections import Counter # # This function counts the unique IP adresses in the logfile # def print_unique_ip(logfile): IPset = set()

我试图使用python 3.3.1计算Apache日志文件中的唯一IP地址 问题是我认为它没有正确地计算每件事

这是我的密码:

import argparse
import os
import sys
from collections import Counter

#
# This function counts the unique IP adresses in the logfile
#
def print_unique_ip(logfile):
    IPset = set()
    for line in logfile:
        head, sep, tail = line.partition(" ")
        if(len(head) > 1):
            IPset.update(head)

    print(len(IPset))
    return  

#
# This is the main function of the program
#
def main():
    parser = argparse.ArgumentParser(description="An appache log file processor")

    parser.add_argument('-l', '--log-file', help='This is the log file to work on', required=True)
    parser.add_argument('-n', help='Displays the number of unique IP adresses', action='store_true')
    parser.add_argument('-t', help='Displays top T IP adresses', type=int)
    parser.add_argument('-v', help='Displays the number of visits of a IP adress')

    arguments = parser.parse_args()

    if(os.path.isfile(arguments.log_file)):
        logfile = open(arguments.log_file)
    else:
        print('The file <', arguments.log_file, '> does not exist')
        sys.exit

    if(arguments.n == True):
        print_unique_ip(logfile)
    if(arguments.t):
        print_top_n_ip(arguments.t, logfile)
    if(arguments.v):
        number_of_ocurrences(arguments.v, logfile)

    return


if __name__ == '__main__':
  main()
但我知道文件中有12个以上的唯一IP

它似乎没有给我正确的结果。我要做的是逐行读取文件,然后当我找到一个IP地址时,我将它放入一个集合中,因为它只保存唯一的元素,然后我打印出所述集合的长度

IPset.update(head)
臭虫。这不会满足你的期望。您希望将每个IP添加到集合中。举例说明如下:

>>> s1 = set()
>>> s2 = set()
>>> s1.add('11.22.33.44')
>>> s2.update('11.22.33.44')
>>> s1
set(['11.22.33.44'])
>>> s2
set(['1', '3', '2', '4', '.'])

知道你期望得到的数字可能会很有用,可能会更快地看到错误。我不知道错误是什么,但是你不能重写
if(not)(len(head))吗1
?我实际上不知道文件中有多少IP,因为它是我通过分配得到的文件,长度为778108行。我只是快速计数,发现超过12个IP。这不是错误。
update
相当于
add
,参数是标量。参数是字符串。是的,我希望这是obvIOU。错误在于OP希望
update
将整个字符串添加到他的
set
中,而实际上它添加了字符串中的每个字符。错误在于他从未打开过文件!
对于日志文件中的行
实际上循环了字符串
openfile
本身中的字符!嗯,不?我明白了
logfile=open(arguments.log_file)
main
中。奇怪的做法,是的,但它会打开文件,而
line
确实是一行。恐怕你完全不正确。错误在于他使用
update
,而不是在打开/迭代文件时。
>>> s1 = set()
>>> s2 = set()
>>> s1.add('11.22.33.44')
>>> s2.update('11.22.33.44')
>>> s1
set(['11.22.33.44'])
>>> s2
set(['1', '3', '2', '4', '.'])