【问题标题】:how to print the output returned from a function in new lines using python?如何使用 python 在新行中打印从函数返回的输出?
【发布时间】:2012-03-26 16:04:57
【问题描述】:

我在日志文件中有 243607 个 ips。函数的输出连续显示唯一的 ips,因此我无法检查输出 ips 是否是唯一的。所以我希望每个 ip 都打印在单独的行中。因为我是 python 新手,所以我无法弄清楚。有什么办法吗?

我还想要打印的 ips 计数

def unique_ips(): 
    f = open('epiclogs.txt','r')
    ips = set(line.split()[0] 
    for line in f:
        if not line.isspace()) 
            ip = line.split()[0] 
            ips.add(ip) 

    return ips

if name__=='__main':
    print unique_ips() 

【问题讨论】:

  • 你能发布你到目前为止的代码吗?
  • def unique_ips(): f = open('epiclogs.txt','r') ips = set(line.split()[0] for line in f if not line.isspace() ) for line in f: ip = line.split()[0] ips.add(ip) return ips if name__=='__main': print unique_ips()
  • 我建议您将代码发布在编辑而不是评论中。

标签: python ip-address


【解决方案1】:

要求不完整:

  1. 日志文件的格式未知。
  2. 输出文件的格式(例如排序?)

我的假设

  1. IP 地址位于第一列
  2. 输出格式应为'[count] [ip address]'

测试数据

10.1.10.190 http://example.com/t1 404
10.1.10.171 http://example.com/t1 404

10.1.10.180 http://example.com/t2 200
10.1.10.190 http://example.com/t1 404
10.1.11.180 http://example.com/t3 302

程序

#!/usr/bin/env python
# 
# Counts the IP addresses of a log file.
# 
# Assumption: the IP address is logged in the first column.
# Example line: 10.1.10.190 http://example.com/t1 404
#

import sys

def extract_ip(line):
    '''Extracts the IP address from the line.
       Currently it is assumed, that the IP address is logged in
       the first column and the columns are space separated.'''
    return line.split()[0]

def increase_count(ip_dict, ip_addr):
    '''Increases the count of the IP address.
       If an IP address is not in the given dictionary,
       it is initially created and the count is set to 1.'''
    if ip_addr in ip_dict:
        ip_dict[ip_addr] += 1
    else:
        ip_dict[ip_addr] = 1

def read_ips(infilename):
    '''Read the IP addresses from the file and store (count)
       them in a dictionary - returns the dictionary.'''
    res_dict = {}
    log_file = file(infilename)
    for line in log_file:
        if line.isspace():
            continue
        ip_addr = extract_ip(line)
        increase_count(res_dict, ip_addr)
    return res_dict

def write_ips(outfilename, ip_dict):
    '''Write out the count and the IP addresses.'''
    out_file = file(outfilename, "w")
    for ip_addr, count in ip_dict.iteritems():
        out_file.write("%5d\t%s\n" % (count, ip_addr))
    out_file.close()

def parse_cmd_line_args():
    '''Return the in and out file name.
       If there are more or less than two parameters,
       an error is logged in the program is exited.'''
    if len(sys.argv)!=3:
        print("Usage: %s [infilename] [outfilename]" % sys.argv[0])
        sys.exit(1)
    return sys.argv[1], sys.argv[2]

def main():
    infilename, outfilename = parse_cmd_line_args()
    ip_dict = read_ips(infilename)
    write_ips(outfilename, ip_dict)

if __name__ == "__main__":
    main()

评论

我喜欢小函数——每个函数都只做一件事。恕我直言,这使程序更易于理解。

【讨论】:

  • 它的工作非常出色。谢谢。
【解决方案2】:

尚未检查您的代码是否有效,但在其中添加了新行,这可以完成您的任务。

试试这个,

def unique_ips(): 
    f = open('epiclogs.txt','r')
    fout = open('uniqueip.txt','w') # Added
    ips = set(line.split()[0] 
    for line in f:
        if not line.isspace()): 
            ip = line.split()[0] 
            ips.add(ip) 
            fout.write("%s\n"%ip) # Added
    f.close() # Added
    fout.flush() # Added
    fout.close() # Added
    return ips

if name__=='__main':
    print unique_ips() 

【讨论】:

    【解决方案3】:

    unique_ips() 返回一个set,这意味着每个 IP 地址只出现一次。如果要在文件中逐行查看地址,可以将print unique_ips() 行更改为:

    if __name__== '__main__':
        f = file('ip_addresses', 'w')
        for ip in unique_ips():
            f.write(ip + '\n')
    

    【讨论】:

    • 它工作正常。但在命令提示符下我只能查看最后 200 个。是否有可能在文本文件中查看整个输出?
    • 我看到你有答案,但我更新了代码以输出到名为 ip_addresses 的文件
    猜你喜欢
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 2016-10-13
    相关资源
    最近更新 更多